Chrome locks with correct JavaScript loop

0

I'm trying to run the following code, but I can not! It's like the browser stops working.

var scores = [60, 50, 60, 58, 54, 54,
58, 50, 52, 54, 48, 69,
34, 55, 51, 52, 44, 51,
69, 64, 66, 55, 52, 61,
46, 31, 57, 52, 44, 18,
41, 53, 55, 61, 51, 44];

for (var i=0; i < scores.length; i = i++) {
    console.log("Buble Solution #" + i + " Score: " + scores[i]);
}

But when I try to execute the synonym with i = i + 1 , it works!

    
asked by anonymous 31.10.2015 / 00:11

1 answer

6

When you use i++ two things happen:

  • i rises by value (sum +1 )
  • this action returns i initial

Well, it returns i initial (before being added plus 1).

Try:

var i = 0;
alert(i++); // 0
alert(i); // 1

That is, in your loop you are making i take the return value of i++ , that is: i = i++ , which is the same as saying i = 0 ... forever. >

Hence the problem.

When you use i = i + 1 , you no longer have a problem, and when you only use i++ (without using i = ), that's fine too.

    
31.10.2015 / 00:20