12 I don't understand


#1



12. Loops and arrays II

What is wrong with my code? Also what is the difference between i+1 and i++? That is confusing me alot.

Thanks

https://www.codecademy.com/courses/javascript-beginner-en-6LzGd/0/4?curriculum_id=506324b3a7dffd00020bf661#

Infinite loops and crashes


var names = ["Taylor","Cam","Kylee","Christian","Sam"];
    for (var i = 0; i > 5; i + 1){
        console.log("I know someone called", names[i]);
    }


#2

Hi you can do

i++
or

i += 1

or 
i = i + 1

but you can't do

i + 1

#4

i+1 will just add one, but don't store it anywhere, where as i++ is a shorthand for i+=1 which on his turn is a shorthand for i = i + 1, which will actually update the i variable, you can see the difference beautiful in the following code:

i = 0
console.log(i);
console.log(i+1);
console.log(i)
i++;
console.log(i)

also, it should be i < 5. The loop will run as long as the condition is true. I will increase until it reaches the end of the list, then it is 5, the condition is false, and the loop will end. i is 0 in the beginning, so i > 5 is false, the loop won't run


#5

Thank you! that helps!


#6

Thanks so much! makes a lot more sense now!