Can't figure out what is going on!


#1

Hi guys,
Just started learning JS a few days back. I'm still lost with all the jargons here. Right now, I followed the instructions but I don't know why there is this
loop();
at the end?
My code works fine without it though:

//Remember to set your condition outside the loop!
var count = prompt("give a number from 1 to 5")

while(count>3){
		//Your code goes here!
		console.log("I'm looping!");
		count--;
	};
	
console.log("Invalid!")

although, there is the number count at the end.. which always ends at 4.
Any help appreciated!

-Eric


#2

It ends at 4 because that is the last integer value greater than 3. The next integer below that is 3, which is not greater than 3, but equal to it. The condition specifically states, count > 3.


#3

Hey there, if you are trying to print the numbers from 5 to 1 using while loop, you should assign the variable as 5 instead of as a prompt.

var count = 5

(Since you put Count-- in the while loop which means decreement) [considering that you put var count = 5 , it should print "I'm looping" twice, since the condition that you provided is that the number should be greater than 3, hence]

But if you are trying to print "I'm looping" or "Invalid" by the prompt that you provide, then you should recall the count variable in the while loop, in order for it to work.

while(count>3) {
console.log("I'm looping");
count = prompt("give a number from 1 to 5");
};
console.log("Invalid!")


This way, when the prompt comes up and you put a value greater than 3, the console will print "I'm looping" , but the prompt will come up again, this will go on, until you put a number lesser than 3 and then it will print "Invalid!"

I'm not sure what you are trying to do with the code but I hope this helps!!
Enjoy coding!


#4

Thanks for the help guys! :slightly_smiling: I've managed to rectify it