While loops issue (How do I loop it 3 times)


#1

So basically I have this:

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

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

loop();

But I don't know how I can have it say "I'm looping" three times


#2

This is what is controlling whether you loop again or not:

while(count)

In your program, count is always 0. You don't have any code that is changing its value.

If you could change its value, say, add one to it each time it loops, then you would have a way of stopping the looping.

Does that give you something to try?


#3

Thanks for your advice but I tried adding "while(count +++ 2)" but that just did an infinite loop.

Do I add "count++" somewhere else then?


#4

That is correct. You need to increment your counter but you must also check its value.

If you make your looping condition be something like:

while(count < 3)

and you increment count somewhere in your loop then you are incrementing and checking -- you need to do both of those.


#5

Alright I tried different codes but it still didn't work, I still don't know what to do.


#6

Show me an example of what you tried where you were:

  1. checking the value of count
  2. incrementing the value of count

#7

I tried again and I finally got it! This is what I put down:

var count = 0

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

loop();

Thanks for your advice!


#8

Hello there, I'm going to jump on the wagon while you're talking about count...

i ran the exact code mentioned above except for one little difference:
I didnt leave the parameters empty for the function but wrote function (count), as the whole function is dependent on the values of count - and that's when it didn't work:

var count=0;

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

loop();

so my question is: why did this destroy my function? everywhere i look a variable function is defined like this:
var x=function(a,b)...

thanks for your insight!


#9

What you have there is a function called loop that is expecting to be sent a parameter called count. You didn't send it that parameter when you called it.

Do you know how to call a function and send it one argument?


#10

hi thanks for the reply, i guess not!
so you mean when i write it like that the function doesn't actually view the "count" as an operation like say a+b but is just waiting for me to give it something to do with the word "count"?

but then, what are the parameters of this function really?
why, actually should the function be empty?

thanks!


#11

Think of it like this, functions just sit there an do nothing until you call them.

If you are going to construct your function to expect some input like the count variable here:

var loop = function(count){

then when you get to the part of your program where you call the function then that's where you send it that input, like this:

loop(3);

#12

This topic was automatically closed after 3 hours. New replies are no longer allowed.