8.When to while and when to for. Can't find what is wrong with my code


#1

var name=true;

var loop= function() {
while (name) {
console.log ('Mira');
name=false;
};

for (var i=0; i<8; i+=2) {
consol.log (i);
}

};

name();


#2

var name=true;
var loop= function() {
   while (name) {
      console.log ('Mira');
      name=false;
};

for (var i=0; i<8; i+=2) {
   consol.log (i); // should be console
}

}; // remove it

name(); // not defined, try loop(); instead

#3

I did the changes but it's still not working..


#4

Please, post your edited code.


#5

var name=true;

var loop= function() {
while (name) {
console.log ('Mira');
name=false;
};

for (var i=0; i<8; i+=2) {
console.log (i);
}

}

loop();


#6

var loop= function() {
   var name=true;
   while (name) {
      console.log ('Mira');
      name=false;
   }

   for (var i=0; i<8; i+=2) {
      console.log (i);
   }
}

loop();

It's because you have infinity while loop. I'm sorry, I forgot replace var name=true; inside loop() function.

Result (from Chrome console):

Mira
0
2
4
6


#7

Thank you :slight_smile:


#8

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