JavaScript > Intro to While Loops > Step 5


#1

Where do I place the count++? This is very confusing...


#2

count should be placed after this line of code: console.log("I'm looping!");

And don't miss semilicon after you initialize your variable count on line 2. Also it's bad practise to declare your variables without a keyword var so I suggest you to fix it and write: var count = 0;


#3


#4

You missed ++ after count


#5

oh my goodness I'm really not trying to make this difficult. Pretty sure I've got everything in there correctly, unless I have to write some more code on the count after the call...


#6

You're doing something wrong in your function. While loop will never start if your count value is equal or greater than 3.


#7

Replace your whole loop function to this:

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

and think why your code didn't execute while loop.


#8

because I didn't code an integer(variable) to equal zero? to be less than the count? Isn't the variable 'count'?


#9

Yes, but count = 0; on line 2 isn't relevant because it's not used anywhere in your code.


#10

Doesn't it disclaim you to not put it in the while loop?


#11

If you want to use value from global variable that is on line 2 then you have to pass that variable when you call loop function, like this: loop(count); and it should work. You declared that variable count but you never used it. When you pass a parameter: loop(count); count initially will be 0 it will work just fine.


#12

@csssolver18247 Some additions to what @elv1nas already said.
About the position of count++. Well best thing to figure out where it should be is to understand what it does and what it does is increasing the value of count by 1.

Where could this be helpful?

Well inside of your while loop, when you start at 0 and increase it by one in every loop it will be at a value of n after n loops. So when you use a condition count < n it will make sure that the loop runs n times. Where n could be any integer number (as you increase by an integer number so far) and is 3 in your case.

Does it matter where it is in the while loop?

The first thing to look for is if this variable is used elsewhere inside the loop. If it is you need to make sure that you still have the features you want e.g. if you want to print the numbers from 0 to 2 you would use

var i =0;
while(i < 2{
    console.log(i);
    i++
}

whereas

var i =0;
while(i < 2{
    i++
    console.log(i);
}

would print the numbers from 1 to 3 because you increased before you print. But as count is used nowhere else your fine to use it anywhere inside your while loop. Side note: Still you might want to place it before console.log() because there is a console feature that echoes the last value of your code and although it is harmless it might be a bit confusing when first encountered.

And about the value of count:

The problem is that you have 2 variables named count inside of your code:
The first one is this global one:

count = 0

or better

var count = 0;

and the other one is this:

var loop = function(count){ ...

What you mention after function in () is treated as if it is declared by var name; inside of the function.

So you have 2 different variables in two different scopes that share nothing but their name. Btw: because they share their name for reasons of avoiding ambiguity only the local variable (that after function) is accessible inside of the function (although it could be if it had a different name) which is called shadowing. The variable outside of the function now has a value of 0 and the variable inside the function has a value of 3 because you assign 3 to it by this function call loop(3);. So you basically have three options (probably many more but 3 common). First you could get rid of the paramter (count) and the argument (3) than you'd only call loop() the global variable would be chosen and your fine for the exercise. But if you have a closer look you see that using only one variable means that after your first function call count would be increased to 3 which basically means that when you try to run the function for a second time you still have the problem that count is already 3 and the loop will not start. So I'd rather recommend as 2nd option something like @elv1nas code:

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

Last but not least you could also use the parameter, in this case you just need to get rid of the global variable count outside of the loop and pass 0 instead of 3 as your argument to the function.


#13

then do count++ each time we console.log() inside the loop. aslong as you have it within the loop.
there are a couple of other things wrong with the numbers you entered

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

loop(/*number*/);

you have the right format just the numbers are wrong. read the excersice over to see where they go. happy coding!


#14

If you have a parameter your first line is redundant.


#15

You're right. the instructions in the lesson basically do say to declare a global variable by not putting it in the while-loop.