Using only html and JavaScript Metric Conversion Program Web Application. (HTML
ID: 3792365 • Letter: U
Question
Using only html and JavaScript
Metric Conversion Program Web Application. (HTML and JavaScript)
Write a Java script that will assist the user with metric conversions.
Your program should allow the user to specify the names of the units as strings (e.g., centimeters, liters, and grams, for the metric system and inches,pounds, for the English system) and should respond to simple questions such as
"How many inches are in 2 meters?"
"How many liters are in 10 quarts?"
Your program should recognize invalid conversions. For example, the question
"How many feet in 5 kilograms?" is not a meaningful question because "feet" is a unit of length whereas "kilograms" is a unit of mass?
Explanation / Answer
<script language="JavaScript" type="text/javascript">
/*The following code accepts a value in a particular unit and then converts in all the different units mentioned in the different rows of the table.
The code deals only with the measurement of the length and not with the measurements involved in mass units.*/
function unitConversion()
{
document.convert.InUnit.focus()
}
var centiValue = 1
var mValue = 100
var kilometValue = 100000
var inchesValue = 2.54000843476
var feetValue = 30.4801012183
var yardValue = 91.440275784
var milesValue = 160934.708789
function toCentiMeters()
{
var i = document.convert.unit.selectedIndex
var thisUnit = document.convert.unit.options[i].value
if (thisUnit == "CENTIMETER")
{
document.convert.cm.value = document.convert.InUnit.value
}
else if(thisUnit == "METER")
{
document.convert.cm.value = document.convert.InUnit.value * mValue
}
else if(thisUnit == "KILOMETER" )
{
document.convert.cm.value = document.convert.InUnit.value * kilometValue
}
else if(thisUnit == "INCHES" )
{
document.convert.cm.value = document.convert.InUnit.value * inchesValue
}
else if(thisUnit == "FEET" )
{
document.convert.cm.value = document.convert.InUnit.value * feetValue
}
else if(thisUnit == "YARD" )
{
document.convert.cm.value = document.convert.InUnit.value * yardValue
}
else if(thisUnit == "MILES" )
{
document.convert.cm.value = document.convert.InUnit.value * milesValue
}
toAll()
}
function toAll()
{
var m = document.convert.cm.value
document.convert.m.value = m / mValue
document.convert.km.value = m / kilometValue
document.convert.inch.value = m / inchesValue
document.convert.ft.value = m / feetValue
document.convert.yd.value = m / yardValue
document.convert.mi.value = m / milesValue
}
//-->
</script>
<div align="center"><h1>Length Converter</h1></div>
<p>Simply choose the unit you want to use, type the value in the field next to it, and click the 'Convert' button.</p>
<div align="center"><form name="convert">
<table border=1>
<tr><th>Unit</th><th>Number</th></tr>
<tr>
<td>
<select name="unit">
<option value="CENTIMETER">CentiMeter
<option value="METER">meter
<option value="KILOMETER">Kilometer
<option value="INCHES">Inch
<option value="FEET">Foot
<option value="YARD">Yard
<option value="MILES">Mile
</select>
</td>
<td>
<input type="text" name="InUnit" size="20" maxlength="20" value="0">
</td>
</tr>
<tr>
<td>
CentiMeter: </td>
<td>
<input type="text" name="cm" size="20" maxlength="20" value="0">
</td>
</tr>
<tr>
<td>
meter:
</td>
<td>
<input type="text" name="m" size="20" maxlength="20" value="0">
</td>
</tr>
<tr>
<td>
Kilometer: </td>
<td>
<input type="text" name="km" size="20" maxlength="20" value="0">
</td>
</tr>
<tr>
<td>
Inch: </td>
<td>
<input type="text" name="inch" size="20" maxlength="20" value="0">
</td>
</tr>
<tr>
<td>
Foot: </td>
<td>
<input type="text" name="ft" size="20" maxlength="20" value="0"> </td>
</tr>
<tr>
<td>
Grams: </td>
<td>
<input type="text" name="g" size="20" maxlength="20" value="0">
</td>
</tr>
<tr>
<td>
Litres: </td>
<td>
<input type="text" name="l" size="20" maxlength="20" value="0">
</td>
</tr>
<tr>
<td>
Pounds: <td>
<input type="text" name="p" size="20" maxlength="20" value="0">
</td>
</tr>
<tr>
<td>
Yard: </td>
<td>
<input type="text" name="yd" size="20" maxlength="20" value="0">
</td>
</tr>
<tr>
<td>
Mile: </td>
<td>
<input type="text" name="mi" size="20" maxlength="20" value="0">
</td>
</tr>
<tr>
<td align="center">
<input type="Reset" value="ReSet">
</td>
<td align="center">
<input type="button" value="Convert">
</td>
</tr>
</table>
</form>
</div>
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.