| Index | Code Tester |
Preventing Bugs
Debugging - Isolating Errors
Common Causes of Errors
1. Identify a source of error other than the ones listed above.
2. What is wrong with the following code?
function displayArray(xArray) {
var i
for (i == 0, i<= xArray.Length, i++) {
document.write(xArray[j] + <br>)
}
Alert("All done")
}
3. What is the following code supposed to do? There are three errors in the code. Fix those errors and test the function in a program. You should use your function for displaying 2D arrays.
function assignArray(N, xArray) {
var i,j;
for (i = 0; i <= N; i++) {
for (j = 0; j <= N; j++) {
if (i = j) {
xArray[i][j] = i*j;};
else {
xarray[i][j] = 0;};
};
};
};
}
4. The following code is supposed to determine if a number is even. Complete the following:
var x = prompt("enter a number", 8)
if (X%2 = "0") alert(It is even)
else Alert("It is odd")
5. The following code is supposed to keep prompting the user for a number until the user enters a number. The function isNaN(arg) is built into JavaScript and stands for isNotANumber. It returns true if the argument (arg) is a number and false if the argument (arg) is not a number. Fix the code. Note that there are no errors associated with isNaN().
var x = hi
while(isNaN(x)){
x = prompt("enter a number,)
}
6. The following function has one error in it. Find the error and then using a for loop, call the function at least 20 times while writing its result to the document. What does the function do? Hint: there is nothing wrong with Math.floor() or Math.random(). these are built-in methods in JavaScript.
function randomize(N1, N2) {
return Math.floor(N2 - N1 + 1)*Math.random() + N1)
}
7. The following code is supposed to ask the user for a number and then add up all numbers between 1 and the number input by the user. There are syntax and logic errors. Fix the code.
var num = prompt(Enter a number)
var total
for (i= =1; i<num;i+ +){
total = i
}
alert(total)
8. Rewrite #7 to use a while loop.
9. The following function is supposed to create a multiplication table with the number of rows and columns passed into it. Fix the code.
function createTable(rows,
cols){ document.write(<table
><tr><td></td>) for(j=1; j<=
col;j++){ document.write("<td><b>" + j +
"</b></td>")
} for(i=1;
i<=rows;i++){ document.write("<tr><td><b>" + i
+"</b></td>") for(j=1;
j<=cols;j++){ document.write("<td>" + i*j +
"</td>")
}
document.write("</tr>")
}
document.write("</table>"
)
10. Set up a page to run the function in #9.
11. Set up a page that asks the user to enter the rows and columns in two prompts. Then call the function.
12. Modify the function in #9 to make the cell widths a fixed size so that all cells are the same width. Also add borders around the cells.
13. In this exercise you will work with another student. Each of you should open up a program and make a change in your program so that an error occurs. You might want to save the modified program as another file. Then move to the other students' computer and try to debug their program.
LINKS AND HELP