0

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!

1
  • 7
    Why +1 in nni<currentSession.Questions.length+1 ? Won't that run over the end of the loop? Maybe that's causing an exception; check the JS console. Commented Apr 12, 2011 at 13:23

3 Answers 3

2

Your nni variable is global because it is not declared with the var keyword:

 function updateScore() {
        var currentPoints = 0;
        // nni declared with var:
        for (var nni = 0; nni < currentSession.Questions.length + 1; nni++) {
            currentPoints += currentSession.Questions[nni].Score;
        }

        alert('hi');
        document.getElementById('quiz_score').innerHTML= '%' + ((currentPoints / currentSession.TotalPoints) * 100)
    }

Also, your evaluation statement is run every time with your incrementation. Move your length evaluation to your declarations:

for (var nni = 0, len = currentSession.Questions.length + 1; nni < len; nni++) {
Sign up to request clarification or add additional context in comments.

1 Comment

I don't think this is a scope issue. Since it's being assigned to = 0, the only way for it to be a scope issue, would be for the variable to be changed outside the loop while the loop is running. I think this is unlikely.
1

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:

function updateScore() {
    try {
        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
    } catch (err) {
        alert('Error ' + err.name + ': ' + err.message);
    }
}

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 nni variable using var.

Comments

1
for (var nni=0;nni<currentSession.Questions.length+1;nni++) {
    currentPoints+=currentSession.Questions[nni].Score;
}

It should be like that, you didnt declare the variable nni. Or didnt you go out of bound?

nni<currentSession.Questions.length+1?

1 Comment

I don't think he's having a scope issue which is all the 'var' will really help with.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.