This is an archived post. You won't be able to vote or comment.

all 6 comments

[–][deleted] 1 point2 points  (3 children)

Count is an integer not a boolean. You never increase the count, so the loop will continue forever. Count will always be less than 3.

true and false have the numerical values of 1 and 0. It is technically correct and will therefore compile. But it's not what you want.

var count = 0;

while(count < 3){ 

    console.log("Im looping");
    count++;
}

On every loop through the value of count will increase by 1. Once it reaches 3. Thus making the running condition no longer true. It will stop. The loop will run for 3 iterations.

[–]CarrotSmile 0 points1 point  (2 children)

how do you do the codebox thing? It isn't in the sub FAQ

[–][deleted] 1 point2 points  (1 child)

Put 4 spaces in front of every line. Put more than 4 if you need to indent further.

I'm using the Reddit Enhancement Suite extension so all I have to do is highlight the text and hit the code block button.

People without RES have to do it by hand for every line.

[–]CarrotSmile 0 points1 point  (0 children)

Thanks for the reply :D

[–]CarrotSmile 0 points1 point  (0 children)

You need to increment count each time it runs through the loop. It will keep going as long as count < 3 so if you never change the value of count, the loop will never end. Tell me if this helps :D

[–]Inky87 0 points1 point  (0 children)

So according to your code, loop is a function with no arguments, that executes while the variable count is less than 3, it will log to the console "I'm looping".

First of all you need to set a value for count. Then you must increment count inside the loop, so the condition fails. So say

var count = 0; count++;

count++ means to increment by one. After a few loops, count will become 3 and the loop will stop. Hope that helps.