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?