Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community!

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Guys help!!!

Guys, I need help with this code, check it out and break it down for me what I did wrong ok. /My purpose of this code is that I want to repeat the same question, as long the question is wrong, but it keeps looping. and my browser is crashed./

var keyword=prompt("What is the passcode? "); if(keyword!=="4444"){ alert("That's wrong passcode");

}else{ alert("Correct"); }do{ keyword++; }while(keyword!=="4444"){ prompt(keyword); }

1 Answer

Hi Aliayub,

Looking at your code there is no break statement, so the code doesn't know when to exit the loop.

To achieve what you want to do, I would have written the following code:

let passcode;

while (passcode !== "4444") {
    passcode = prompt("What is the passcode?");
    if (passcode !== "4444"){
        alert ("Sorry, that's incorrect. Please try again.")
    } else {
        alert("Correct");
        break;
    };
}
  • So while the passcode is not "4444", you will get a prompt asking you to enter a passcode.
  • Once you enter a passcode there's then an if else statement that runs.
    • If the passcode is not "4444" You get an alert stating that it's incorrect and to try again. As there is no more code to execute, we go back to the start of the while loop.
    • If the passcode is "4444" then you get an alert stating that it is correct, and then there's a break statement to exit the loop.

The issue with the above is that it can be very frustrating for a user to be stuck in a loop they can't exit. I'm sure you've felt the same frustration whilst writing this code. So it's best practice to allow the user to be able to break the loop. To remedy this I would have written the following code.

let passcode;

while (passcode !== "4444") {
    passcode = prompt("What is the passcode?");
    if (passcode === null) {
        break;
    } else if (passcode !== "4444") {
        if (confirm("Sorry, that's incorrect. Would you like to try again?.")) {
            continue;
        } else {
            passcode = null;
            break;
        }
    } else {
        alert("Correct");
        break;
    };
}

The additions here, let the user cancel out of the prompt. Also, rather than an alert forcing the user to try again, they now get a confirmation box, asking them if they would like to continue. Clicking cancel in either of these will now exit the loop and result in the passcode variable being null.

You can see an example of the above here on my codepen.

Hope this helps.

On a side note, be sure to checkout the Markdown Cheatsheet for examples on how to format your post and make it easier for others to read your example code. You'll find the link for this below the text area when adding answers and asking questions.

thanks man