<< Chapter < Page | Chapter >> Page > |
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 coding error 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 thatyou are expecting to see.
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. While the diagnostic information provided in thoseconsoles is limited, it will usually indicate the line number in the source code where the programming error was detected. Knowing the line number will help youexamine the code and fix the error.
An exercise involving multiplication
Please copy the code shown in Listing 2 into an html file and open it in your browser.
Listing 2 . An exercise involving multiplication. |
---|
<!-- File JavaScript02.html --><html><body><script language="JavaScript1.3">//Compute and display the product of three
// numbers, each having a different number// of significant figures.
var a = 169.01var b = 0.00356
var c = 386.253var product = a * b * c
document.write("product = " + product + "<br/>")
//Round the product to the correct number// of significant figures
var rounded = product.toPrecision(5)document.write("rounded = " + rounded + "<br/>")
//Display a final line as a hedge against// unidentified coding errors.
document.write("The End")</script></body></html> |
The screen output
When you open your html file in your browser, the text shown in Figure 3 should appear in your browser window.
Figure 3 . Screen output from Listing #2. |
---|
product = 232.39900552679998
rounded = 232.40The End |
The code in Listing 2 begins by declaring three variables named a , b , and c , multiplying them together, and displaying the product in the browser window. Each of the factors in theproduct have a different number of significant figures, with the factor of value 169.01 having the least number (5) of significant figures. We knowfrom rule #2 , therefore, that we need to present the result rounded to five significant figures.
The toPrecision method
Listing 2 calls a method named toPrecision on the variable named product , passing the desired number of significant figures (5) as a parameter. The method rounds the value stored in product to the desired number of digits and returns the result, which is stored in the variable named rounded . Then the contents of the variable named rounded are displayed, producing the second line of text in Figure 3 .
Notification Switch
Would you like to follow the 'Game 2302 - mathematical applications for game development' conversation and receive update notifications?