0

So I have this code:

function resetAll(einde) {
if(einde !=1) {
    if(beurt == 0) {
        spelerbolletje.gewonnen += 1;
        document.getElementById("bolletje").value = spelerbolletje.gewonnen;
    } else {
        spelerkruisje.gewonnen += 1;
        document.getElementById("kruisje").value = spelerkruisje.gewonnen;
    }
    }
    setVakjes();
    resetVakjes();
}

somehow this sets the value for the value of the winner from 0 to NaN. does someone know how to solve this?

3
  • how is spelerbolletje declared? Commented Jan 22, 2016 at 10:57
  • @Tomas at the top of the document as var spelerbolletje; Commented Jan 22, 2016 at 11:02
  • Check out Shitsu's answer. Incrementing a variable that has not been defined would not work correctly. Commented Jan 22, 2016 at 11:04

1 Answer 1

1

What you do seems correct. I think the problem is that XX.gewonnen has not been initialized.

var a = {};
a.gewonnen += 1; // => gewonnen = NaN

So, ensure gewonnen is defined:

if(XX.gewonnen == undefined) XX.gewonnen = 0;
// rest of your code
Sign up to request clarification or add additional context in comments.

5 Comments

all needed variables are declared at the top of the document or when they are needed, so XX.gewonnen has been declared at the top since XX is needed all over the document
Is it declared like XX.gewonnen;, or is it given the value 0 like XX.gewonnen = 0;?
function fSpeler(naam, beurt, gewonnen) { this.naam = naam; this.beurt = beurt; this.gewonnen = gewonnen; }
nevermind, thanks, it was indeed because XX.gewonnen was not given a value at the beginning, thank :D
That's cool and all, but is it, at some point, actually given a value? If it is only declared as XX.gewonnen;, then it is simply a place in memory where a value can be stored. If it's declared as XX.gewonnen = 0;, then JavaScript knows that the value is numeric, so it can be incremented. EDIT: Ah okay, graag gedaan!

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.