5. Practice Makes Perfect Keeps Crashing


#1


https://www.codecademy.com/courses/javascript-beginner-en-ASGIv/1/1?curriculum_id=506324b3a7dffd00020bf661


Browser keeps crashing


I don't know what's going on with my code; it keeps crashing my browser.

var count = 0;

var loop = function(){
	while(count < 3){
		console.log("I'm looping!");
		count = count++;
	}
};

loop();


#2

I'm not sure what this line executes as. There is a way to alter the code on this line that would allow your loop to run in a way that fulfills the exercise requirements.


#3

@cloudplayer19815

count = count++; //  count is 0 = infinityloop

I try to explain it

// Postfix
var x = 0;
var y = x++; // y = 0, x = 1

// Präfix
var x = 0;
y = ++x; // x = 1, y = 1

That means:

count = ++count; // solve the issue
count++; // solve the issue too
++count; //Again is solved

#4

This doesn't work. I ran it on replit and jsbin. Infinite loop.

Like you mentioned here.


#5

That must be an error on jsbin :wink:

var count = 0;

var loop = function(){
	while(count < 3){
		console.log("I'm looping!");
		count = ++count;
        console.log(count); 
	}
};

loop();

Try it at jsbin, you will see no infinite Loop.
And than try it without console.log in Firefox or Chrome and here at codeacademy and you will see it worked.


#6

I see it now. When count = ++count, it runs. I don't get how. Count = count++ gives an infinity loop. I don't get why.


#7

How i can explain it better?
Maybe the defintion from MDN helps you

The increment operator increments (adds one to) its operand and returns a value.

If used postfix, with operator after operand (for example, x++), then it returns the value before incrementing.

count = count++; // count is 0

If used prefix with operator before operand (for example, ++x), then it returns the value after incrementing.

count = ++count; // count is 1

#8

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.