<< Chapter < Page Chapter >> Page >

Using JavaScript to convert units

Let's write, execute, and analyze a script that solves the same problem.

The objective of the script is to convert a distance of 2112 paces to a distance in miles where it is given that

  • 100 paces = 3000 inches
  • 12 inches = 1 foot
  • 5280 feet = 1 mile

Please copy the code from Listing 1 into an html file and open the file in your browser.

Listing 1 . Convert from paces to miles.
>!-- File JavaScript01.html ------------------------------>>html>>body>>script language="JavaScript1.3">var d = 2112 //distance, units = paces document.write("d = " + d + " paces" + ">/br>") var d2 = 3000 //distance, units = inchesvar d3 = 100 //distance, units = paces var f1 = d2/d3 //factor, units = inches/pacedocument.write( "f1 =" + f1 + " inches/pace" + ">/br>") //pace*inch/pace = inchvar d = d *f1 //distance, units = inches document.write("d = " + d + " inches" + ">/br>") var f2 = 1/12 //factor, units = feet/inchdocument.write( "f2 = " + f2 + " feet/inch" + ">/br>") //inch*feet/inch = feetvar d = d *f2 //distance, units = feet document.write("d = " + d + " feet" + ">/br>") var f3 = 1/5280 //factor, units = miles/footdocument.write( "f3 = " + f3 + " miles/foot" + ">/br>") //feet*mile/feet = milevar d = d *f3 //distance, units = miles document.write("d = " + d + " mile" + ">/br>") //Now reverse the process using reciprocals//(pace/inch)*(inch/foot)*(foot/mile) = pace/mile var f4 =(1/f1)*(1/f2)*(1/f3) //factor, units = paces/miledocument.write( "f4 = " + f4 + " paces/mile" + ">/br>") //miles*paces/mile = pacesvar d = d *f4 //distance, units = paces document.write("d = " + d + " paces" + ">/br>") document.write("The End")>/script>>/body>>/html>

Screen output

When you open the html file in your browser, the text shown in Figure 5 should appear in your browser window.

Figure 5 . Screen output for Listing #1.
d = 2112 paces f1 =30 inches/paced = 63360 inches f2 = 0.08333333333333333 feet/inchd = 5280 feet f3 = 0.0001893939393939394 miles/footd = 1 mile f4 = 2112 paces/miled = 2112 paces The End

Discuss the output first

Let's begin our discussion with the screen output shown in Figure 5 and then go back and examine the code that produced that output.

Figure 5 begins with a distance (labeled d ) expressed in units of paces. The value is 2112 paces, which is the distance from your home to your school discussed earlier.

Convert paces to inches

Then Figure 5 shows the conversion factor (labeled f1 ) that can be used to convert a value expressed in paces to a value expressed in inches.

That conversion factor is applied to the original distance producing an output consisting of the same distance expressed in inches instead of paces. Thedistance is now 63360 inches.

Convert from inches to feet

Following that, f2 shows the conversion factor that can be used to convert a value expressed in inches to the same value expressed in feet.

That conversion factor is applied to the current distance value producing an output consisting of the same distance expressed in feet instead of inches. Thedistance is now 5280 feet.

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Accessible physics concepts for blind students. OpenStax CNX. Oct 02, 2015 Download for free at https://legacy.cnx.org/content/col11294/1.36
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Accessible physics concepts for blind students' conversation and receive update notifications?

Ask
Prateek Ashtikar
Start Quiz
Marion Cabalfin
Start Test
Joanna Smithback
Start Quiz
Dravida Mahadeo-J...
Start Quiz
Vanessa Soledad
Start Exam
Heather McAvoy
Start Quiz