Technologies:
Tolerim
a day ago
How do I resolve a particular bug in the "Hangman Game" as someone who is new to JavaScript?
<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)
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.