choudlette
choudlette

Reputation: 1

Problems with Javascript Decreasing Value and Quitting Loop

I am having trouble with this hangman game that I am building. I am adding basic functionality so that if you do not guess a correct letter then a guessNumber variable decreases by one. The problem I am having with my current code is that when a player guesses an incorrect letter - it quits the while loop altogether. Am I structuring either my while loop or the positioning of the guessNumber--;? I have been tinkering around with this code for about an hour and still cannot figure this out!

<!DOCTYPE html>
<html>
<head>
    <title>Hangman</title>
</head>
<body>
<h1>Hangman</h1>
<script>
// array of words
var words = [
"trajectory", "symphony", "desire", "antfarm", "dancer", "happiness", "positioning",
"hobbit", "obituary", "cheetah", "sunrise", "antithesis", "wrong", "diamonds",
"partnership", "oblique", "sanctuary"];

// pick a random word

var word = words[Math.floor(Math.random() * words.length)];

// set up the answer array
var answerArray = [];

for (var i = 0; i < word.length; i++) {
    answerArray[i] = "_";
}

var remainingLetters = word.length;

//amount of guesses

var guessNumber = 5;

//the game loop

while (remainingLetters > 0 && guessNumber > 0) {
    //show the player their progress
    alert("Your word is " + answerArray.join(" ") + "and you have " +guessNumber+ " guesses left");

    //get a guess from player
    var guess = prompt("Guess a letter, or click cancel to stop playing.");
    if (guess === null) {
        //exit the loop
        alert("Ok you can quit");
        break;
    } else if (guess.length !== 1) {
        alert("Please enter a single letter.");
    } else
        //update the game state with the guess
        for (var j = 0; j < word.length; j++) {
            if (word[j] === guess) {
                answerArray[j] = guess;
                remainingLetters--;
            }
        } else {
        guessNumber--;
        }
    }
    //end game loop

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

</script>
</body>
</html>

Upvotes: 0

Views: 73

Answers (1)

Jecoms
Jecoms

Reputation: 2848

You were decrementing guessNumbers each time the loop didn't find the letter. The guessNumbers-- needs to be outside of the loop so it only reduces the guess number once per input.

// array of words
var words = [
"trajectory", "symphony", "desire", "antfarm", "dancer", "happiness", "positioning",
"hobbit", "obituary", "cheetah", "sunrise", "antithesis", "wrong", "diamonds",
"partnership", "oblique", "sanctuary"];

// pick a random word

var word = words[Math.floor(Math.random() * words.length)];

// set up the answer array
var answerArray = [];

for (var i = 0; i < word.length; i++) {
    answerArray[i] = "_";
}

var remainingLetters = word.length;

//amount of guesses

var guessNumber = 5;

//the game loop

while (remainingLetters > 0 && guessNumber > 0) {
    //show the player their progress
    alert("Your word is " + answerArray.join(" ") + "and you have " +guessNumber+ " guesses left");

    //get a guess from player
    var guess = prompt("Guess a letter, or click cancel to stop playing.");
    if (guess === null) {
        //exit the loop
        alert("Ok you can quit");
        break;
    } else if (guess.length !== 1) {
        alert("Please enter a single letter.");
    } else {
        //update the game state with the guess
        for (let j = 0; j < word.length; j++) {         
            if (word[j] === guess) {
                answerArray[j] = guess;
                remainingLetters--;
            }
         }
         guessNumber--;
    }
} //end game loop

//alert to congratulate player
alert(answerArray.join(" "));
if (remainingLetters === 0) alert("Good job! The answer was " + word);
else alert("No more guesses! The answer was " + word);
<!DOCTYPE html>
<html>
<head>
    <title>Hangman</title>
</head>
<body>
<h1>Hangman</h1>
  
  
</body>
<html>

Upvotes: 2

Related Questions