It works, but is it right?


#1

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


This works, but is it right? I've seen some variations like ex:

var count = 0;

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

loop();

Thanks for you help in advance!


var count = 0;

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

loop();


#2

Yes they would both work but as you're asking the question I'd assume that you're not quite sure what exactly these 2 are doing, are you? So maybe add some more context about what you already know and what might not be as clear.

Also think what happens when you call the function twice:

loop();
loop();

and maybe think about this:

var loop = function(){
    var count = 0;
    while...
};

loop();

#3

In programming they are numerous ways to perform the same tasks. So it all comes down to preference and understanding. :slight_smile:

So yes,
that code is as you put it right.


#4

Well yeah I have an idea, but I'm not completely sure about the difference. So here is what I think. That what I put down basically says count up and log it while it's less than four. The other says while less than three log it then count up. My reasoning for this is that even though they sound the same the order is different; meaning its sequence is: log, up, log, up, log, up instead of up, log, up, log, up, log. Also I'm kinda knew to this, but this really cool. Thanks for the help wanted to confirm my thoughts with someone else.


#5

Thanks for the input.


#6

Well it's not so much about the log as it is about the condition. Because the console.log doesn't depend on count but the condition does. So it wouldn't be a big deal to swap these:

console.log("I'm looping!"); 
count++;

count++;
console.log("I'm looping!");

Whereas here:

count++, count < 4

The order is crucial. Because in an expression of the from

statement1, statement2

The first statement is just executed and the second statement is executed as well and provides a value for the whole expression of statement1, statement2. That's why count < 4 remains the relevant condition although you add something before it. But as the first part is executed as well the value of count is already increased before the condition is checked therefore you start at 1 before the first loop and not at 0 and therefore you need to adjust the boundary value from 3 to 4.

Hope this makes sense.

Btw: If you know the number of loops you want to do it's probably easier to use a for loop than to modify a while loop to emulate a for loop.


#7

I see that makes sense, I was thinking about that when I posted the second question. A condition for the while loop vs the condition depending on the count. So it would seem that I just made it have two conditions to count up and run while less than four. With the other I just gave it the condition to run while less than thee then log and count up. I think I understand what your saying. Again thanks this clearing things up.


#8

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