Java Reference
In-Depth Information
Chapter 4
Exercise 1 Question
The example debug_timestable2.htm has a deliberate bug. For each times table it creates only multi-
pliers with values from 1 to 11 .
Use the script debugger to work out why this is happening, and then correct the bug.
Exercise 1 Solution
The problem is with the code's logic rather than its syntax. Logic errors are much harder to spot and
deal with because, unlike with syntax errors, the browser won't inform you that there's such and such
error at line so and so but instead just fails to work as expected. The error is with this line:
for (counter = 1; counter < 12; counter++)
You want the loop to go from 1 to 12 inclusive. Your counter < 12 statement will be true up to and
including 11 but will be false when the counter reaches 12 ; hence 12 gets left off. To correct this, you
could change the code to the following:
for (counter = 1; counter <= 12; counter++)
Exercise 2 Question
The following code contains a number of common errors. See if you can spot them:
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN”
“http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml”>
<head>
<title>Chapter 4, Question 2</title>
</head>
<body>
<script type=”text/javascript”>
function checkForm(theForm)
{
var formValid = true;
var elementCount = 0;
while(elementCount =< theForm.length)
{
if (theForm.elements[elementcount].type == “text”)
{
if (theForm.elements[elementCount].value() = “”)
alert(“Please complete all form elements”)
theForm.elements[elementCount].focus;
formValid = false;
break;
}
Search WWH ::




Custom Search