Home > database >  Cannot break out the while loop when I want to
Cannot break out the while loop when I want to

Time:11-05

I just started learning JAVASCRIPT a few days ago and I got stuck in a while loop exercise. So the prompt should return what I wrote in it (Like a parrot), and that works just fine, but the thing is I would like for the while loop to print the "console.log" and break whenever the "if" or "else if" statements are true and cant seem to get that right. I know there is no "break" in the code below but I would appreciate if someone could tell me where it should go, as I tried several places and none seem to work.

let parrotRobot = prompt("Hi, Im a robot parrot");

while (true) {
    parrotRobot = prompt(parrotRobot);

    if (parrotRobot.toLowerCase() === "stop copying me") 
    { console.log("Aight, Geen probleem");}
    else if (parrotRobot.toLowerCase() === "stop acting like a parrot") 
    { console.log("Ok, I liked being a parrot though");}

}

CodePudding user response:

Besides break,you can use a boolean flag in while()

let parrotRobot = prompt("Hi, Im a robot parrot");
let flag = true
while (flag) {
    parrotRobot = prompt(parrotRobot);

    if (parrotRobot.toLowerCase() === "stop copying me") { 
       flag = false;
       console.log("Aight, Geen probleem");
     }else if (parrotRobot.toLowerCase() === "stop acting like a parrot") { 
       flag = false;
       console.log("Ok, I liked being a parrot though");
     }

}

CodePudding user response:

Your while loop is checking if the boolean true has a true or 'truthy' value. Since true is always going to be 'true', you need a loop stopper, like @lucumt suggested a flag seems like the best option. If you're constantly checking if a value is true and nothing inside your code does anything to change that, your value will always be true and therefore the loop will never stop.

Also you're redeclaring the 'parrotRobot' variable twice in your code, once in the global scope and for a second time inside your loop, meaning you have two different variables named the same.

Global and local scope means that any variable declared inside a function/loop/object/method will only be available inside that function/loop/object/method (local scope). If you'd try calling it from outside it would return a Reference Error (meaning it doesn't exist). Any variable outside of any function/loop/object/method is in the global scope.

If you create a variable in the global scope and a new one with the same name inside any function/loop/object/method the computer won't know which one you're referring to. Javascript scope article.

//a global scope example

let parrotName = "Fred";

// code here can use parrotName

function myFunction() {
// code here can also use parrotName but not create a variable called parrotName
}

Overall you code should look something like this

let parrotRobot = prompt("Hi, Im a robot parrot");
let message = true;

while (message) {
    //avoids redeclaration
    parrotMessage = prompt(parrotRobot);

    if (parrotMessage.toLowerCase() === "stop copying me") {
         console.log("Aight, Geen probleem");
        message = false;
    }else if (parrotMessage.toLowerCase() === "stop acting like a parrot") {
        console.log("Ok, I liked being a parrot though");
        message = false;
    }
};

  • Related