I have, in multiple parts of my code, snippits similar to this:
function updateScore() {
var currentPoints=0;
for (nni=0;nni<currentSession.Questions.length+1;nni++) {
currentPoints+=currentSession.Questions[nni].Score;
}
alert('hi');
document.getElementById('quiz_score').innerHTML='%'+(currentPoints/currentSession.TotalPoints)*100
}
Everything works fine… until after the loop. This happens in multiple cases. The alert won’t even show after the loop ends. It’s like the function just stops…
I’m also having problems where the iterator (in this case, nni) stays global. Basically, I can’t use that variable ever again in my code because for some reason, if I change nni, it messes up the for loop. I’m obvisouly not doing something right. I’m a self tought Javascripter (basically googling anything I don’t know, I’ve never taken a lesson). I must be missing something about for loops.
Thanks if you can!
What does the JS console report?
If you’ve no idea what the JS console is, Google it, or add an exception handler to your function:
Also, you’re letting the loop go too far; you should stop at .length, and not at .length + 1.
And as has been noticed by others: you should really declare your
nnivariable usingvar.