[Solved] Why does “break;” not allow me to exit my loop like it should?


Put the break inside the loop

var words = [
            "javascript",
            "monkey",
            "amazing",
            "pancake"
        ];
var word = words[Math.floor(Math.random() * words.length)];
var answerArray = [];
for (var i = 0; i < word.length; i++) {
            answerArray[i] = "_";
        }
var remainingLetters = word.length;
while (remainingLetters > 0) {
            alert(answerArray.join(" "));
            var guess = prompt("Guess a letter, or click Cancel to stop playing.");
if (guess === null) {
    break;
} else if (guess.length !== 1) {
    alert("Please enter a single letter.");
} else {
    for (var j = 0; j < word.length; j++) {
        if (word[j] === guess) {
            answerArray[j] = guess;
            remainingLetters--;
        }
    }
}
        }

alert(answerArray.join(" "));
alert("Good job! The answer was " + word);

0

solved Why does “break;” not allow me to exit my loop like it should?