I'm currently working through a Javascript tutorial from a book that guides me in creating a hangman game using only functions. I'm almost at the end but I've hit a roadblock... My function seems to not be looping properly, specifically, my prompt message stops after the first input and doesn't continue for additional guesses, even though it should as part of the loop implementation. It's a simple issue, but I can't seem to get past it. If someone could take a look, I would appreciate it.
I've attempted to use "console.log" within an "if" statement and inside a "for" loop to check if the loop is functioning correctly. The loop works, but the prompt doesn't repeat as expected. I'm unsure why this is happening...
var updateGameState = function (guess, word, answerArray) {
// Update answerArray and return a number indicating how many times
// the guess appears in the word so remainingLetters can be updated
for (var j = 0; j < word.length; j++) {
if (word[j] === guess && answerArray[j] !== word[j]) {
answerArray[j] = guess;
remainingLetters--;
};
}; getGuess();
console.log(answerArray);
};
var remainingLetters = word.length;
var correctGuesses = updateGameState(guess, word, answerArray);
Currently, I just need my prompt message to keep popping up until I'm able to input all my letters/guesses...
----Below are the .js and html files----
var pickWord = function(random) {
// Retrieve a random word
return random[Math.floor(Math.random() * random.length)];
};
var word = pickWord(["fakultet", "zirafa", "trava", "labelo"]);
console.log(word);
var setupAnswerArray = function(word) {
// Generate the answer array
var answerArray = [];
for (i = 0; i < word.length; i++) {
answerArray[i] = "_";
}
console.log(answerArray);
return answerArray;
};
var answerArray = setupAnswerArray(word);
var showPlayerProgress = function(answerArray) {
// Use alert to display the player their progress
return alert(answerArray.join(" "));
};
showPlayerProgress(answerArray);
var getGuess = function() {
// Utilize prompt to fetch a guess
return (prompt("Guess a letter, or click Cancel to stop playing " + answerArray)).toLowerCase();
};
var guess = getGuess();
console.log(guess);
// PROBLEM OCCURS HERE
///*******************************************************
var updateGameState = function(guess, word, answerArray) {
// Modify answerArray and provide a count of how many times
// the guess appears in the word to update remainingLetters
for (var j = 0; j < word.length; j++) {
if (word[j] === guess && answerArray[j] !== word[j]) {
answerArray[j] = guess;
remainingLetters--;
};
};
getGuess();
console.log(answerArray);
};
var remainingLetters = word.length;
var correctGuesses = updateGameState(guess, word, answerArray);
//*********************************************************
/* - code from the book
var showAnswerAndCongratulatePlayer = function (answerArray) {
// Display the answer using alert and congratulate the player
};
var word = pickWord();
var answerArray = setupAnswerArray(word);
var remainingLetters = word.length;
while (remainingLetters > 0) {
showPlayerProgress(answerArray);
var guess = getGuess();
if (guess === null) {
break;
} else if (guess.length !== 1) {
alert("Please enter a single letter.");
} else {
var correctGuesses = updateGameState(guess, word, answerArray);
remainingLetters -= correctGuesses;
}
}
showAnswerAndCongratulatePlayer(answerArray);
*/
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Section 2: JavaScript Language Basics</title>
</head>
<body>
<h1>Section 2: JavaScript Language Basics</h1>
</body>
<script src="script.js"></script>
</html>