<< 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.

Questions & Answers

what is biology
Hajah Reply
the study of living organisms and their interactions with one another and their environments
AI-Robot
what is biology
Victoria Reply
HOW CAN MAN ORGAN FUNCTION
Alfred Reply
the diagram of the digestive system
Assiatu Reply
allimentary cannel
Ogenrwot
How does twins formed
William Reply
They formed in two ways first when one sperm and one egg are splited by mitosis or two sperm and two eggs join together
Oluwatobi
what is genetics
Josephine Reply
Genetics is the study of heredity
Misack
how does twins formed?
Misack
What is manual
Hassan Reply
discuss biological phenomenon and provide pieces of evidence to show that it was responsible for the formation of eukaryotic organelles
Joseph Reply
what is biology
Yousuf Reply
the study of living organisms and their interactions with one another and their environment.
Wine
discuss the biological phenomenon and provide pieces of evidence to show that it was responsible for the formation of eukaryotic organelles in an essay form
Joseph Reply
what is the blood cells
Shaker Reply
list any five characteristics of the blood cells
Shaker
lack electricity and its more savely than electronic microscope because its naturally by using of light
Abdullahi Reply
advantage of electronic microscope is easily and clearly while disadvantage is dangerous because its electronic. advantage of light microscope is savely and naturally by sun while disadvantage is not easily,means its not sharp and not clear
Abdullahi
cell theory state that every organisms composed of one or more cell,cell is the basic unit of life
Abdullahi
is like gone fail us
DENG
cells is the basic structure and functions of all living things
Ramadan
What is classification
ISCONT Reply
is organisms that are similar into groups called tara
Yamosa
in what situation (s) would be the use of a scanning electron microscope be ideal and why?
Kenna Reply
A scanning electron microscope (SEM) is ideal for situations requiring high-resolution imaging of surfaces. It is commonly used in materials science, biology, and geology to examine the topography and composition of samples at a nanoscale level. SEM is particularly useful for studying fine details,
Hilary
cell is the building block of life.
Condoleezza Reply
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

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