I'm working on a node word guessing game and I'm trying to have code that prompts the user for input until they run out of guesses or get the word right. Right now I'm having an issue working with prompt's callback. Because of the nature of callbacks my code runs through all the "prompt" code for each word before finally waiting for user input. I've tried using a forEach and while loop originally. Later tried setting up promises, but I haven't figured anything out.
for(i = 0; i < wordsInPlay.length; i++){
if(losses < 3){
let solved = false;
wordThisRound = new Word(wordsInPlay[i]);
wordThisRound.setupWord();
getGuess();
}
}
Here's the getGuess function implements the prompt nod.js package:
function getGuess() {
wordThisRound.updateWordDisplay();
if (wrongsLeft > 0) {
new Promise(function(resolve) {
prompt.get(guessObject, function(err, result) {
console.log('line 68');
if(err) throw err;
let userGuess;
const letterGuessed = result.guess.toLowerCase();
if (wordThisRound.wordObject.hasOwnProperty(letterGuessed) && wordThisRound.wordObject[letterGuessed].guessed === false) {
userGuess = true;
wordThisRound.wordObject[letterGuessed].guessed = true;
wordThisRound.wordObject[letterGuessed].changeDisplay();
}
else {
userGuess = false;
wrongsLeft--;
}
resolve({gussedRight: userGuess, letter: letterGuessed});
});
}).then(function(promise){
console.log('Inside .then function.');
new Promise(function(resolve){
gameFeedback(promise.gussedRight, promise.letter);
resolve();
});
}).then(function(){
getGuess();
});
}
else console.log("You're our of wrong guesses...You lost this round.");
}