In: Computer Science
HTML/CSS/JavaScript
Create a page that converts 4 different measurements (i.e. length, volume, area, distance) to either Imperial or Metric values. When a value is entered into one of the fields, the converted amount is displayed in the other corresponding field.
Example:
Converting between litres and gallons. If you enter 25 for gallons, the value of 113.50 litres should be displayed.
Converting between gallons and litres. If you enter 75 litres, the value of 16.52 gallons should be displayed. (Note, 1 Imperial gallon is 4.54 litres)
Example:
Converting between metres and feet. If you enter 125 for feet, the value of 38.10 metres should be displayed.
Example:
Converting square metres to feet. If you enter 95 for square metres, the value of 1022.57 square feet should be displayed.
Your page will require JavaScript ‘event handlers’ that will respond to changes in the number field and call the correct functions. Your answers should display to 2 significant digits. Your page must use CSS to create an interesting and pleasing interface
HTML Page :
<!DOCTYPE html>
<htmL>
<style>
input[type=text] {
padding: 12px;
border: 2px solid teal;
border-radius: 4px;
resize: vertical;
font-family:cursive;
}
h4 {
font-family: verdana;
}
h1 {
font-family: verdana;
}
</style>
<body>
<center>
<!--Page Header -->
<h1>Unit Convertor<h1>
<!-- creating header and two input field for meter and feet -->
<!-- oninput function called when user input some value in input fields -->
<!-- this.value specify current input field value -->
<!-- first parameter of oninput function is input value and second is destination unit -->
<h4>Convert between Meter and Feet</h4>
<input type="text" id="meter" oninput="distanceConversion(this.value,'feet')" placeholder="Value in Meter">
<input type="text" id="feet" oninput="distanceConversion(this.value,'meter')" placeholder="Value in Feet">
<!-- creating header and two input field for liter ang gallon -->
<h4>Convert between Liter and Gallon</h4>
<input type="text" id="liter" oninput="volumeConversion(this.value,'gallon')" placeholder="Value in Liter">
<input type="text" id="gallon" oninput="volumeConversion(this.value,'liter')" placeholder="Value in Gallon">
<!-- creating header and two input field for sq meter and sq feet -->
<h4>Convert between Square Meter and Square Feet</h4>
<input type="text" id="sq-meter" oninput="areaConversion(this.value,'sq-feet')" placeholder="Value in Square Meter">
<input type="text" id="sq-feet" oninput="areaConversion(this.value,'sq-meter')" placeholder="Value in Square Feet">
<!-- creating header and two input field for inch and Feet -->
<h4>Convert between Inch and Feet</h4>
<input type="text" id="inch-length" oninput="lengthConversion(this.value,'feet-length')" placeholder="Value in Inch">
<input type="text" id="feet-length" oninput="lengthConversion(this.value,'inch-length')" placeholder="Value in Feet">
<!-- javascript code -->
<script>
<!-- distanceCoversion is used to convert between feet and meter-->
<!-- toFixe(2) is used to display upto two decimal place -->
function distanceConversion(value,destination) {
if(destination == 'feet') {
// update feet input field value
document.getElementById("feet").value = (value * 3.28).toFixed(2);
}
else if(destination == 'meter') {
// update meter field value
document.getElementById("meter").value = (value / 3.281).toFixed(2);
}
}
<!-- volumeConversion is used to convert between gallon and liter -->
function volumeConversion(value,destination) {
if(destination == 'gallon') {
// update gallon field value
document.getElementById("gallon").value = (value / 4.54).toFixed(2);
}
else if(destination == 'liter') {
// update liter field value
document.getElementById("liter").value = (value * 4.54).toFixed(2);
}
}
<!-- areaConversion is used to convert sq feet and sq meter -->
function areaConversion(value,destination) {
if(destination == 'sq-feet') {
// update sq feet field value
document.getElementById("sq-feet").value = (value * 10.764).toFixed(2);
}
else if(destination == 'sq-meter') {
// update sq meter field value
document.getElementById("sq-meter").value = (value / 10.764).toFixed(2);
}
}
<!-- lengthConversion is used to convert between gallon and liter -->
function lengthConversion(value,destination) {
if(destination == 'feet-length') {
// update feet field value
document.getElementById("feet-length").value = (value / 12).toFixed(2);
}
else if(destination == 'inch-length') {
// update inch field value
document.getElementById("inch-length").value = (value * 12).toFixed(2);
}
}
</script>
</body>
</html>
Sample Output :
Please refer to the Screenshot
of the code given below to understand indentation of the code
: