-1

First day of school, we're supposed to make a hangman game. I've been staring at the logic in my while loop for hours now. I can't get my loop to say yes, the word(newWord) does contain the guess. I always get the prompt that it is incorrect, and then all heck breaks loose. I've tried 25 different ways. I know it's all busted beyond repair now, but if anyone can get me going the right direction, I'd be eternally grateful.

  let words = ["skate", "guitar", "laugh", "party", "shirt"]

let wordValue = Math.floor(Math.random() * 4) + 1;

let newWord = words[wordValue];

let misses = 0;
let rightGuess = 0;
let wrongGuess = 0;
var answerLines = [];
for (let i = 0; i < newWord.length; i++) {
    answerLines[i] = "_";
}
    let remainingLetters = newWord.length;



    while (remainingLetters > 0 && misses < 6) {
        alert(answerLines.join(" "));
        let guess = prompt("Guess a letter, any letter!");


            for(let j = 0; j < newWord.length; j++) {
                if (newWord[j] === guess) {
                    rightGuess++
                }
                else { wrongGuess++ }
                if (rightGuess != 0) {

                    answerLines[j] = guess;
                    remainingLetters--;
                }



            else {
                misses++
                (alert("That was is incorrect.  You have " + misses + " of 6 misses."));
ScottVMeyers
  • 307
  • 3
  • 15

1 Answers1

0

Your logic for processing the guess is a little off. Try this:

var words = ["skate", "guitar", "laugh", "party", "shirt"]

var wordValue = Math.floor(Math.random() * 4) + 1;

var newWord = words[wordValue];

console.log("Word is: " + newWord);

var misses = 0;
var answerLines = [];

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

var remainingLetters = newWord.length;


while (remainingLetters > 0 && misses < 6) {
    alert(answerLines.join(" "));
    var guess = prompt("Guess a letter, any letter!");

    var matchesNone = true; //boolean to track if guess matched any letters

    for (var j = 0; j < newWord.length; j++) {

        if (newWord[j] === guess) {
            answerLines[j] = guess;
            remainingLetters--;
            matchesNone = false; //the guess matched atleast one letter
        }
    }

    if (matchesNone) { //the guess matched none of the letters
        misses ++;
        alert("That was is incorrect.  You have " + misses + " of 6 misses.");
    }
}

if(remainingLetters>0) {
    alert("You failed to guess the word: " + newWord);
}else{
    alert("You guessed the word!  It was: " + newWord);
}
strider
  • 5,674
  • 4
  • 24
  • 29
  • thank you so much for taking the time! I haven't worked with booleans in this way yet. That was exactly what I needed. Very useful tool. thanks again!! – ScottVMeyers Nov 10 '15 at 07:58
  • your welcome, try to stick with `var` instead of `let`. Here's the difference between the two if you're interested [http://stackoverflow.com/questions/762011/let-keyword-vs-var-keyword](http://stackoverflow.com/questions/762011/let-keyword-vs-var-keyword) – strider Nov 10 '15 at 08:04
  • I'd love to but my teacher is pretty strict about this. He thinks var is for baby javascript and wants to teach us grown-up javascript. – ScottVMeyers Nov 11 '15 at 05:33