5. Practice makes perfect, Introduction to while Loops? Different way of doing


#1


https://www.codecademy.com/courses/javascript-beginner-en-ASGIv/1/1?curriculum_id=506324b3a7dffd00020bf661#


I understand that this isn't the way this exercise is supposed to be done but I thought I'd try it this way anyway.I thought that by doing it this way the while would loop until it had reached the number that was put into the function. Please explain where i am going wrong, Thanks.


var loop = function(number){
	while(for(var i=0; i<number; i++)){
		
		console.log("I'm Looping")
	}
};

loop(3);


#2

This is not a conditional expression. The while statement takes a condition. Your for parameters contain a conditional... i < number, so write just that into your while ()

var i = 0
while (i < number) {
    // code
    i++;
}

The above is all inside the function body. Another way would be to decrement number

while (number > 0) {
    // code
    number--;
}

or

while (number) {    // non-zero number is truthy
    // code
    number--;
}

or

while (number--) {    // zero is falsy
    // code
}

Again, each would make up the body of the function.


#3

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