Java Reference
In-Depth Information
</body>
</html>
Chapter 18
exercise 1 Question
The example
ch18 _ example4.html
has a deliberate bug. For each times table it creates only
multipliers 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 (var 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 (var 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>
<html lang="en">
<head>
<title>Chapter 18: Question 2</title>
</head>
<body>
<form name="form1" action="">
<input type="text" id="text1" name="text1" />
<br />
CheckBox 1<input type="checkbox" id="checkbox2" name="checkbox2" />
<br />
CheckBox 1<input type="checkbox" id="checkbox1" name="checkbox1" />
<br />
<input type="text" id="text2" name="text2" />
<p>
<input type="submit" value="Submit" id="submit1"
name="submit1" />
</p>
</form>
<script>
function checkForm(e) {