Back

Technologies:

javascriptjavascript
htmlhtml
avatar
Tolerim
a day ago

How do I resolve a particular bug in the "Hangman Game" as someone who is new to JavaScript?

Good Morning All,

I am a beginner in JavaScript and have been learning from various resources, including the book "JavaScript For Kids". I'm currently on "Chapter 7: Creating a Hangman Game". I was able to complete the first three challenges of "Programming Challenges" but stuck on the fourth one named "Fixing a Bug". The bug in the game exists in a situation where if the same correct letter is selected again, "remainingLetters" keeps decrementing. Could you please resolve this issue? According to the challenge, I can solve this problem by adding another condition to check whether a value in "answerArray" is still an underscore. If it's not an underscore, then that letter must have been guessed already, but I'm unsure of where to start.

I have provided the code below. I tried researching but couldn't find a solution.

<script>
    //Setting a maximum number of tries.
    var maximumTries = 10;
    
    //Creating an array of words.
    var words = [
        "quail",
        "chicken",
        "kookaburra",
        "parrot"
    ];
    
    //Selecting a random word.
    var word = words[Math.floor(Math.random() * words.length)];
    
    //Setting up the answer array.
    var answerArray = [];
    for (var i = 0; i < word.length; i++) {
        answerArray[i] = "_";
    }
    var remainingLetters = word.length;
    
    //Array to hold all the guessed letters.
    var guessedLettersArray = "";
    
    //The game loop.
    while (remainingLetters >= 0 && guessedLettersArray.length < maximumTries) {
        //Displaying the player's progress.
        alert(answerArray.join(" "));
        
        //Getting the input letter from the player.
        var guessedLetter = prompt("Guess a letter or click cancel to stop playing.");
        guessedLettersArray += guessedLetter;
        guessedLetter = guessedLetter.toLowerCase();
        if (guessedLetter === null) {
            // Exit the loop if the player clicks cancel.
            break;
        } else if (guessedLetter.length !== 1) {
            alert("Please enter a single letter only.");
        } else {
            var letterFound = false;
            for (var j = 0; j < word.length; j++) {
                if(word[j] === guessedLetter)  {
                    if(answerArray[j] === '_') {
                        answerArray[j] = guessedLetter;
                        remainingLetters--;
                        letterFound = true;
                    } else {
                        letterFound = true;
                    }
                }
            }
            if(!letterFound) {
                guessedLettersArray.length--;
            }
        }
    }
    //Displaying the answer and congratulating the player.
    alert(answerArray.join(" "));
    alert("Good job! The answer was " + word);
</script>
Answers(1)
avatar
Tolerim
a day ago
Verified Answer
The issue in the code is that if the correct letter is guessed multiple times, the remainingLetters variable will decrement by more than 1. To fix this, you can add another condition to the inner if statement that checks if the value in answerArray at index j is still an underscore. If it is not, that means the letter has already been guessed and the remainingLetters variable should not be decremented. Here's the updated code:
// Set a maximum number of tries
var maximumTries = 10;

// Create an array of words
var words = [
  "quail",
  "chicken",
  "kookaburra",
  "parrot"
];

// 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;

// This will hold all the letters tried
var guessAll = "";

// The game loop
while (remainingLetters > 0 && guessAll.length < maximumTries) {
  // Show the player their progress
  alert(answerArray.join(" "));

  // Get a guess from the player
  var guess = prompt("Guess a letter, or click Cancel to stop playing.");
  guessAll += guess;
  guess = guess.toLowerCase();
  if (guess === null) {
      // Exit the game loop
      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] === "_") {
              answerArray[j] = guess;
              remainingLetters--;
          }
      }
  }
}

// Show the answer and congratulate the player
alert(answerArray.join(" "));
alert("Good job! The answer was " + word);
Now, the remainingLetters variable will only decrement by 1 if a correct letter is guessed multiple times.
;