<< Chapter < Page Chapter >> Page >

An exercise involving addition

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

Listing 1 . An exercise involving addition.
<!-- File JavaScript01.html --><html><body><script language="JavaScript1.3">//Compute and display the sum of three // numbersvar a = 169.01 var b = 0.00356var c = 385.293 var sum = a + b + cdocument.write("sum = " + sum + "</br>") //Round the sum to the correct number// of digits to the right of the decimal // point.var round = sum.toFixed(2) document.write("round = " + round + "</br>") //Display a final line as a hedge against// unidentified coding errors. document.write("The End")</script></body></html>

Screen output

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

Figure 2 . Screen output from Listing #1.
sum = 554.30656 round = 554.31The End

The code in Listing 1 begins by declaring three variables named a , b , and c , adding them together, and displaying the sum in the JavaScript default format in the browser window.

Too many decimal digits

As you can see from the first line in Figure 2 , the result is computed and displayed with eight decimal digits, five of which are to the right of thedecimal point. We know, however, from rule #1 , that we should present the result rounded to a precision of two digits to theright of the decimal point in order to match the least precise of the numbers included in the sum. In this case, the value stored in the variable named a is the least precise.

Correct the problem

The code in Listing 1 calls a method named toFixed on the value stored in the variable sum passing a value of 2 as a parameter to the method. This method returns the value from sum rounded to two decimal digits. The returned value is stored in the variablenamed round . Then the script displays that value as the second line of text in Figure 2 .

The output text that reads "The End"

There is a downside to using JavaScript (as opposed to other programming languages such as Java). By default, if there is a codingerror in your script, there is no indication of the error in the output in the main browser window.Instead, the browser simply refuses to display some or all of the output that you are expecting to see. (Remember, I told you that JavaScript is not my favoriteprogramming language, but it is probably the most accessible for blind students who have no programming experience.)

Put a marker at the end

Writing the script in such a way that a known line of text, such as "The End" will appear following all of the other output won't solve coding errors.However, if it doesn't appear, you will know that there is a coding error and some or all of the output text may be missing.

JavaScript and error consoles

I explained how you can open a JavaScript console in the Google Chrome browser or an error console in the Firefox browser in an earlier module titled JavaScript for Blind Students . While the diagnostic information provided in those consoles is limited, it will usually indicate the line number in the source codewhere the programming error was detected. Knowing the line number will help you examine the code and fix the error.

Questions & Answers

what is the stm
Brian Reply
is there industrial application of fullrenes. What is the method to prepare fullrene on large scale.?
industrial application...? mmm I think on the medical side as drug carrier, but you should go deeper on your research, I may be wrong
How we are making nano material?
what is a peer
What is meant by 'nano scale'?
What is STMs full form?
scanning tunneling microscope
how nano science is used for hydrophobicity
Do u think that Graphene and Fullrene fiber can be used to make Air Plane body structure the lightest and strongest. Rafiq
what is differents between GO and RGO?
what is simplest way to understand the applications of nano robots used to detect the cancer affected cell of human body.? How this robot is carried to required site of body cell.? what will be the carrier material and how can be detected that correct delivery of drug is done Rafiq
what is Nano technology ?
Bob Reply
write examples of Nano molecule?
The nanotechnology is as new science, to scale nanometric
nanotechnology is the study, desing, synthesis, manipulation and application of materials and functional systems through control of matter at nanoscale
Is there any normative that regulates the use of silver nanoparticles?
Damian Reply
what king of growth are you checking .?
What fields keep nano created devices from performing or assimulating ? Magnetic fields ? Are do they assimilate ?
Stoney Reply
why we need to study biomolecules, molecular biology in nanotechnology?
Adin Reply
yes I'm doing my masters in nanotechnology, we are being studying all these domains as well..
what school?
biomolecules are e building blocks of every organics and inorganic materials.
anyone know any internet site where one can find nanotechnology papers?
Damian Reply
sciencedirect big data base
Introduction about quantum dots in nanotechnology
Praveena Reply
what does nano mean?
Anassong Reply
nano basically means 10^(-9). nanometer is a unit to measure length.
do you think it's worthwhile in the long term to study the effects and possibilities of nanotechnology on viral treatment?
Damian Reply
absolutely yes
how to know photocatalytic properties of tio2 nanoparticles...what to do now
Akash Reply
it is a goid question and i want to know the answer as well
characteristics of micro business
for teaching engĺish at school how nano technology help us
How can I make nanorobot?
Do somebody tell me a best nano engineering book for beginners?
s. Reply
there is no specific books for beginners but there is book called principle of nanotechnology
how can I make nanorobot?
what is fullerene does it is used to make bukky balls
Devang Reply
are you nano engineer ?
fullerene is a bucky ball aka Carbon 60 molecule. It was name by the architect Fuller. He design the geodesic dome. it resembles a soccer ball.
what is the actual application of fullerenes nowadays?
That is a great question Damian. best way to answer that question is to Google it. there are hundreds of applications for buck minister fullerenes, from medical to aerospace. you can also find plenty of research papers that will give you great detail on the potential applications of fullerenes.
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

Get the best Algebra and trigonometry course in your pocket!

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?