0

Alright so here's a class with a six item array in the constructor.

class forLoopProblem {

  constructor() {
    this.a = [5,8,7,4,6,18];
  }

What I want to do is use the length of the array to limit the number of iterations in the for loop below

  iterate(ap1) {
    for (i = 0; i <= this.a.length; i++) {
      console.log(i);
    }
  }
}

var internalVar = new forLoopProblem();

Unfortunately internalVar.iterate() produces an exception stating that a is not defined.

  log() {
    console.log(this.a.length);
    console.log(this.a);
  }
}

var internalVar = new forLoopProblem();

But if a is not defined then why does internalVar.log() behave as expected, printing 6 along with the contents of the array to the console?

At first I thought perhaps 6 is a string that needs to be converted to an integer before the loop assignment will recognize it. So I tried parseInt() but that didn't work. And anyway the exception says that the variable isn't defined, so I don't think it's a parsing issue.

I can even use a to assign the initial iteration variable to 6, like so:

    for (i = this.a.length; i <= 10; i++) {

to produce 6,7,8,9, and 10 as console output.

So what gives with the limiter specification?

Doctor.S
  • 11
  • 3
  • Please fix *all* the typos in your code before posting it here. – Bergi Jun 28 '16 at 13:25
  • I only see one, where I misspelled iterate. And as long as it's consistent that shouldn't matter. – Doctor.S Jun 28 '16 at 13:31
  • `itterate`, `interate`, `lenth`… And I hope the forgotten declaration of the `i` variable is a typo as well. – Bergi Jun 28 '16 at 13:45
  • Oh I see those now. Will correct. But no, as it turns out the forgotten declaration of i was not a typo. The official JavaScript tutorial fails to mention that the i must be declared. And in literally every situation different from this, I've never been required to declare it. So I had no idea. And I'm currently trying to figure out the why of it. – Doctor.S Jun 28 '16 at 13:54
  • Well you just a had lot of luck… not declaring variables makes them global, and in iterations that can easily lead to infinite loops. – Bergi Jun 28 '16 at 14:02

1 Answers1

1

Works for me, though i had to define i in the for-loop:

JavaScript

class forLoopProblem {
  constructor() {
    this.a = [5,8,7,4,6,18];
  }
  itterate(ap1) {
    for (var i = 0; i <= this.a.length; i++) {
      console.log(i);
    }
  }
}

var internalVar = new forLoopProblem();
internalVar.itterate(); //0, 1, 2, 3, 4, 5, 6

Tested in:

  • Chrome - Works
  • Firefox - Works
  • Opera - Works
  • IE10 - Does not work
  • Safari (Win8) - Does not work
Arg0n
  • 8,283
  • 2
  • 21
  • 38
  • That worked thanks. Why do you think that makes a difference though? I've never needed to declare i as a variable in any other situation. – Doctor.S Jun 28 '16 at 13:40
  • Maybe inside ES6 `class` you have to? (Just guessing). – Arg0n Jun 28 '16 at 13:42
  • [classes are strict mode code](http://stackoverflow.com/a/29285330/1048572), and if you [assign to an undeclared variable](http://blog.niftysnippets.org/2008/03/horror-of-implicit-globals.html) that is an exception. It should show up in the error console ([example](http://stackoverflow.com/q/23549062/1048572)). – Bergi Jun 28 '16 at 13:46