-5

So jsLint says my x is not defined. I looked it up everywhere but I cant find how to define it...

for (x = 0; x < verdachten.length; x++) {
       console.log("De verdachte is de  " + verdachten[x].leeftijd + "jaar oud " + verdachten[x].naam  + ", de " + verdachten[x].wie);
    }

That's where it goes wrong. The x...

2 Answers2

2

This is a case of jsLint being a bit overcautious. Most browsers will automagically define the x, but jsLint warns of this as it's easy to get a scoping bug if you don't properly init your variables, like this:

for( var x = 0; x < verdachten.length; x++ ) {
  console.log(
    "De verdachte is de " +
    verdachten[x].leeftijd +
    "jaar oud " +
    verdachten[x].naam +
    ", de " +
    verdachten[x].wie
  );
}

Problems can arise if you have an x defined somewhere else within scope:

function doStuff() {
  var x = "derp";

  // things
  console.log(x); //=> "derp";

  for(x = 0; x < 100; x++) {
    // other things
    console.log(x);//=> 0..99
  }

  console.log(x); //=> 99
  // original x variable has now changed :(
}
thykka
  • 540
  • 2
  • 12
0
for (var x = 0; x < verdachten.length; x++) {
       console.log("De verdachte is de  " + verdachten[x].leeftijd + "jaar oud " + verdachten[x].naam  + ", de " + verdachten[x].wie);
    }

for (var x = 0; x < verdachten.length; x++) {

You have the problem in defining the variable 'x'. In javascript variables are defined by prefix var and do not require variable type.

Happy Programming :)

Abdul Baig
  • 3,683
  • 3
  • 21
  • 48