How to loop this three times


#1


var android = 0

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

};

loop();


Replace this line with your code. 



#2

Hey @mtap52190,

You should instead use a variable, and iterate over it while it is less than a certain value...


#3

Well, this is how I loop that 3 times. If you think it's good, you should try:
var loop = function(count) {
while(count<3) {
count++;
console.log("I'm looping!");
}
};
loop(0);


#4

why do we add that last zero in between the loop brackets?


#5

I believe that he added that there by accident, so just ignore it. He was just calling the loop function and must have accidentally hit 0 or something of the sort.


#6

Actually the 0 in the parenthesis of loop is a good idea, so hopefully it was not done by accident.
Maybe review the part about functions and parameters.

What happens is that the argument (the value in the parenthesis: here 0) gets assigned to the parameter (the value in the parenthesis after the function keyword: here count) so here the loop starts from 0 due to the 0 in the function call. That actually makes a lot of sense, because unfortunately a lot of folks build functions that can only run once, while this could work as often as you need it.


#7

Oh wow, I didn't know that you could do it that way; I learn something new every day don't I :slight_smile:


#8

yeah I played around with it and figured out what was going on but if I already put count in the brackets in between the function, shouldn't that work by itself since I've already declared initially set count to 0 and increasing it by 1. I tried the loop without adding 0 at the bottom but it didn't work.


#9

I just declared count as a variable, made the loop, and then called the loop.

And I just realized what @haxor789 was pointing at, I had just gone brain dead :slight_smile:


#10

by adding it in the parenthesis after function you declared it right. Now it has a value of undefined and using ++ on count means undefined+1 which is NaN. You might be able to leave the loop with this but it is probably not what you intended to do, do you?

Also could you post the code your talking about?


#11

Actually, I'm a girl. Please don't call me "he". And I'm from Vietnam. So glad to have some one who is also interested in coding. :slight_smile:


#12

hey it's cool. I realized typed it in again and it worked. Must've done something wrong the first time. Thanks a lot for the help though.


#13

Thanks a lot, I didn't know I could use count++ solo like that. I thought it had to be inside the parenthesis like in all the other examples. I need to start thinking outside the box.


#14

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