10 - "To learn it, you gotta 'do' it"


#1

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

I don't see why my code's messing up. I think it has something to do with the true/false 'legit' complex but I don't see why. My while loop will only run if 'legit' is true, and inside the while loop is a statement that makes 'legit' false. It's self-terminating, basically. Despite this, it still continues on infinitely. Why?

Of course this might not be the actual case. I just need help, really.


var legit = true;

var getToDaChoppa = function(){
  do {
      console.log("Now");
  }
  while(legit = true) {
      console.log("Now");
      legit = false;
  };
};

getToDaChoppa();


#2

Try changing
while(legit = true)
into
while(legit === true)


#3

Didn't work.

var legit = true;

var getToDaChoppa = function(){
  do {
      console.log("Now");
  }
  while(legit === true) {
      console.log("Now");
      legit = false;
  };
};

getToDaChoppa();

results in my browser crashing.


#4

Okay! During a Do/While the {} part of a While condition is replaced by the {} part of a Do condition.
First Do {} happens. Then, if While() is true, Do {} happens again and again until While() is false.

So, in your code,

console.log("Now");
legit = false;

never happens, it just keeps going back to

do {
console.log("Now");
}


#5

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