Loops and arrays 2.question. why did it print 6 times instead of five?


#1

// Click on "Stuck? Get a hint!" if you get stuck!

var names = ["jack","james","sydney","luther","kyle"];
for (i = 0; i <= 5; i++){
console.log("I know someone called " + names[i]);
}


#2
0, 1, 2, 3, 4, 5

That's six iterations. Change your loop constraint to,

for (var i = 0; i < names.length; i++) {

}

Now it will set the upper bound to the length of the array, minus 1. Length is always one more than the highest index of an array. In the above, the highest index is 4, so names.length is 5.