Write a JavaScript program that converts a distance in miles into a distance in
ID: 3568145 • Letter: W
Question
Write a JavaScript program that converts a distance in miles into a distance in meters. One
mile is equal to 1.60935 kilometers. If you multiple the number of miles times 1.60935, you will
calculate the number of kilometers. Declare miles as a variable and give it a value. Declare
kilometers as a variable. Use the formula above for converting miles to kilometers to assign a
value to kilometers. Display the results in an alert box. Include the original number of miles and
the calculated distance in kilometers in the message displayed in the alert box.
Explanation / Answer
Step 1: Insert the below script into the <HEAD> section of your page:
<script type="text/javascript">
var factors1 = new Array(1, 0.01, 0.00001, 0.00000621, 0.3937, 0.0328, 0.01094);
var factors2 = new Array(100, 1, 0.001, 0.000621, 39.37, 3.28, 1.094);
var factors3 = new Array(100000, 1000, 1, 0.621, 39370, 3280, 1094);
var factors4 = new Array(160934, 1609.34, 1.60934, 1, 63360, 5280,1760);
var factors5 = new Array(2.54, 0.0254, 0.0000254, 0.0000158, 1, 0.08333, 0.02778);
var factors6 = new Array(30.48, 0.3048, 0.0003048, 0.0001896, 12, 1, 0.3333);
var factors7 = new Array(91.44, 0.9144, 0.0009144, 0.0005688, 36, 3, 1);
var factors = new Array(factors1,factors2,factors3,factors4,factors5,factors6,factors7);
function convert_unit()
{
from_index = document.length_con.from_unit.selectedIndex;
to_index = document.length_con.to_unit.selectedIndex;
factor = factors[from_index][to_index];
document.getElementById("formula").innerHTML = document.length_con.from_unit.options[document.length_con.from_unit.selectedIndex].text + " = " + factor + " " + document.length_con.to_unit.options[document.length_con.to_unit.selectedIndex].text;
if(isNaN(document.length_con.from_value.value))
document.getElementById("to_value").innerHTML = "Not a valid number.";
else
document.getElementById("to_value").innerHTML = factor * document.length_con.from_value.value;
}
</script>
Step 2: Insert the below HTML block in the <BODY> section of your page:
<h2>Length Converter</h2>
<p>
<b>1</b> select a length unit from the "From" dropdown list and one from the "To" dropdown list.
<br /><b>2</b> Enter a numeric value, and click "To" button.
<p><form name="length_con">
<table>
<tr>
<td>From </td>
<td>
<select name=from_unit;>
<option> centimeters
<option> meters
<option> kilometers
<option> miles
<option> inches
<option> feet
<option> yards
</select>
</td>
<td> To </td>
<td>
<select name=to_unit;>
<option> centimeters
<option> meters
<option> kilometers
<option> miles
<option> inches
<option> feet
<option> yards
</select>
</td>
</tr>
<tr>
<td> </td><td colspan="3"><div id="formula">centimeters = 1 centimeters</div></td>
</tr>
<tr>
<td colspan="4"> </td>
</tr>
<tr>
<td>Enter </td>
<td><input type="text" name="from_value" value="1" size="12" maxlength="12"></td>
<td> <input type=button value="To";> </td>
<td><div id="to_value">1</div></td>
</tr>
<tr>
<td><div align="center">This free script provided by<br />
<a href="http://www.javascriptkit.com">JavaScript Kit</a></div></td></tr>
</table>
</form>
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.