Practice Makes Perfect: Count < 4 works, not Count < 3?


#1



I don't understand why the coding below works, but I'm told that this code I wrote is successful. Specifically, I changed the count inside of the while loop code, so that count < 4. The instructions in this lesson tell me to set count < 3, so that it logs "I'm looping" three times to the console, which would make sense, since the count starts with 0, so that it would log it three times (0, 1, and 2) and stop before getting to the third time. However, I had to set 'count < 4' to pass this lesson, and I don't understand why. Can someone help me understand?


//Remember to set your condition outside the loop!
var count = 0;

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

loop(count < 3);


#2

There should not be a conditional expression in the function call. It will pass true instead of 0.

In practice we would define the count variable inside the function so it is a local variable and so it gets reset to 0 every time the function is called.

... function () {
        count = 0
        while (...) {
            count++;    // do this before logging
            console.log(" ... ");
        }
    }

Setting count before logging prevents the unexpected screen output at the end.


#3

Is this an example of good code for this project?

//Remember to set your condition outside the loop!
var count = 0;

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

loop(count === 3);


#4

Two things, the count variable is still outside of the function. This is not a good practice in real world code. Is it in the instructions to write it this way and pass it to the function? If not, then include it in the function and remove the parameter.

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

The other is the expression you are passing as an argument. It is a boolean expression that passes false to the function. If written for the above, the call will be,

loop();

Alternatively if we wish to be able to pass in a count limit, then we could write it like,

 var loop = function (lim) {
    var count = 0;
    while (count < lim) {
        count++;
        // code
    }
}
loop(3);

#5

Mtf, thanks for showing me what the code would best look like in the real world. Codecademy specifically instructs me to put the count variable outside of the function and pass it to the function, as the first line of code on the page. I think it's that way because of a lesson on local vs. global scope, but in this case, there is no need for the variable to be included in the global scope. I will start putting the variable inside the function to develop good habits.

I don't completely understand what you mean when you say, "It is a boolean expression that passes false to the function." However, I appreciate you showing me how to pass in a count limit and will use that from now on.


#6
count === 3    // since count === 0 this is false

loop(false);

There is some weirdness that takes place when we assign a boolean to a variable and then increment it.

 > a =  false
=> false
 > a++
=> 0
 > a++
=> 1

false is almost the same as zero, but we see here that JavaScript did not immediately convert false to 0 until after we incremented it the first time. That means the loop could iterate twice in roughly the same state when it should have only iterated once.

This is something that will come up later in your studies of data types and conversion by coercion. What we have done above is a form of coercion where we forced JS to make a conversion in the background.

A more explicit way to convert a boolean to a number is with a unary expression.

+false    // 0
+true     // 1

However, this in no way actually fits this lesson so best we put this on hold until later.


#7

I am so in envy of your knowledge right now. I can't wait to expand my knowledge of JavaScript to this level.


#8

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