Enter the average.html text from Figure 17.12 into a new Web page, then load this page to
Question:
Enter the average.html text from Figure 17.12 into a new Web page, then load this page to verify that it behaves as described. What happens if you click on the button without entering any numbers in the text box?
Calling the split method on an empty string produces an empty array, i.e., an array with length 0. Thus, clicking the button in this page without entering any numbers will result in a call to the Average function with an empty array as input. When the function attempts to calculate the average of an empty array, a division by zero occurs, causing the function to return NaN. Modify the Average function so that it returns 0, rather than NaN, if its input is empty. Then verify that your average.html page displays 0 for the average if the user leaves the text box empty?
Step by Step Answer: