Reputation:
This is a little external js script for my web programming class, which is supposed to prompt the user for hours worked for employees, until they enter a negative number, when it prints a table with the appropriate number of rows (the number of employees for which hours were entered for) with a column for hours worked and a column for wage. But something about the for loop at the end is freezing the js engine in my browser. If I comment the loop out, the prompts work fine. If I put in a nonsense while loop which prints something to the screen, the prompts still work and then the while loop runs. But something about this stupid for loop just freezes the whole script, and opening the HTML page literally does nothing. Blank page. The assignment is already late and I'm at my wits end.
var empHours = 0;
var numEmployees = 0;
var empWage = 0;
var totalWages = 0;
var empWages = new Array();
do {
empHours = prompt("Enter number of hours employee worked this week.");
if (empHours > -1) {
numEmployees++;
if (empHours <= 40) {
empWage = (15 * empHours)
empWages[numEmployees-1] = empWage;
totalWages += empWage;
} else {
empWage = (15 * 1.5 * empHours);
empWages[numEmployees-1] = (15 * 1.5 * empHours);
totalWages += empWage;
}
}
} while (empHours > -1);
confirm("You have " + numEmployees + " employees, is this correct?");
var root = document.getElementById('tablediv');
var table = document.createElement('table');
var row, cell;
for (i=0; i<=numEmployees; i++) {
document.write("Made it to for loop"):
row = table.insertRow(i);
for (j=0; j<3; j++) {
cell = row.insertCell(j);
cell.innerHTML(empWages[i])
}
}
root.appendChild(table);
Upvotes: 1
Views: 380
Reputation: 707258
Prompt returns null or a string. It does not return a number.
I would suggest you change to:
while (empHours && parseInt(empHours, 10) >= 0)
This guards against null
and converts the string to a number before checking to see if it's negative.
Also, I would suggest you NOT use document.write()
for debugging, ever. It can really mess up an existing page if used at the wrong time. You should be using console.log()
or an actual debugger.
Upvotes: 1