Stuck on Intro to 'While' Loops in JS


#1

Instructions:
Write a while loop that logs "I'm looping!" to the console three times. You can do this however you like, but NOT with three console.log calls. Check the Hint if you need help!

var loop = function(){
while(){
//Your code goes here!
}
};

loop();

-I have no clue where to start. The hint says:
We actually know we need to loop three times, so we could use a for loop, but we'll use while this time.

If we create a variable called count and set it to 0 outside the loop (on line 2), then do count++ each time we console.log() inside the loop, we'll be able to track how many loops we've made. If we set the while condition to be count < 3, that should do the trick!

-but im still stuck. Please help.


#2

//count set to 0 outside the loop
var count = 0;

var loop = function(){
	while(count < 3){ //sets the count less than three having set it = 0
		//prints the string to show how many loops. Count is now 0,1,2
		console.log("I'm looping!"); count++;
	}
};

loop(); //call the function

Hope this helps! :slight_smile:


#3

Yes, it worked and your explanation definitely does help! Thank you!


#4

You're welcome.

Have fun coding! :slight_smile:


#5

Thank you!! I didn't even have to post and your explanation was exactly what I needed! Thank you!


#6

You're welcome.

Have fun coding! :slight_smile:


#7

thank you it really helped.:yum:


#8

You're welcome @oscarabundez.

Have fun coding! :slight_smile:


#9

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