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

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