When to 'while" and when to "for" 8/11


#1


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


It appears I'm creating an infinite loop, however I don't see how that's the case. Any help would be appreciated.


var action = true; 
var mission = function (){
while (action = true){
    console.log ("Jump up and down!");
    var action = false; 
}
}; 
mission(); 

var women = ["JLo", "Angelina", "Eva", "Hally"];
for (var i = 0; i < women.length; i++) {
    console.log (" I want to be with " + " " + women [3]);
};


#2

for tends to be more useful when the number of loops is known in advance and you need to set up a counter for the loop.

while is more suitable for doing something until a condition is met as opposed to a fixed number of times.

They can both do what the other one can do. Your infinite loop is due to using a condition that always evaluates to true, consider what the = operator does (look it up, don't guess or assume)


#3

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