'How can I stop messages from printing more than once inside of an if statement?

I am trying to create a hangman game where the user guesses a three-letter word. I have a variable called "guesses". I have my code set up to where if the user doesn't guess a letter that is in the word (chosen from a word list) then I have ++ for guesses. Depending on what guess the user is on, a message pops up saying that the letter is not in the word and they have however many guesses left. But what I noticed is that if a letter is guessed incorrectly, then after if it is guessed correctly, then the previous message still pops up this is my code I've tried incorporating a break or return statement, but it just stops it altogether. can anyone please help?

function hangManGame() {
  var wordList = ["cat"];
  var guesses = 0;

  var randomWord = wordList[Math.floor(Math.random() * wordList.length)];
  while (randomWord) {
    var letterGuessed = readLine("Please guess ONE lowercase letter at a time. ");
    if (letterGuessed == randomWord[0]) {
      println("Congratulations, you guessed a letter correctly!");
    } else if (letterGuessed == randomWord[1]) {
      println("Congratulations, you guessed a letter correctly!");  
    } else if (letterGuessed == randomWord[2]) {
      println("Congratulations, you guessed a letter correctly!");
    } else {
      guesses++;
    }
    
     if (guesses == 1) {
      println("Sorry, that letter is not in the word, you have 2 guesses remaining.");
      add(head);
      add(body);
    }
     if (guesses == 2) {
      println("Sorry, that letter is not in the word, you have 1 guess remaining.");
      add(leftArm);
      add(rightArm);
        
    }
    if (guesses == 3) {
      println("Sorry, that letter is not in the word, you have 0 guesses remaining.");
      add(leftLeg);
      add(rightLeg);
    break;
    }
    
    
   }
  }


Solution 1:[1]

Your solution has some major flaws:

  1. It only works for 3 letter words
  2. You duplicate your code multiple times
  3. It also doesn't work at all you can type in letter "c" three times in a row and win.

this is my solution you can adapt it to your "needs" as I provided in comments

// the responseArray is to simulate the readLines as I wanted to run the code in the browser console
function hangManGame(word, tries = 3, responseArray = ['c', 'a', 't']) {
  // getting the first from array instead of reading user input
  while (responseArray.length > 0 && tries > 0) {
    const guessedLetter = responseArray.shift()
    // make it generic instead of having to rewrite the if over and over
    // if the word contains our letter
    if (word.indexOf(guessedLetter) != -1) {
      console.log(`correct guess: ${guessedLetter}`)
    } else {
      console.log(`Wrong latter: ${guessedLetter} tries remaining ${--tries}`)
    }
  }
}

hangManGame('cat', 3, ['x', 'a', 'f', 'g'])

AS provided in comments the response array is only there to simulate your user inputs so just remove it and instead of assigning guessedLetter variable to responseArray.shift() assign it to readLine(). If you have any questions add them in comment ;)

Still you should probably add a way for person to see the word they typing as in true hangman which would look something like this

// the responseArray is to simulate the readLines as I wanted to run the code in the browser console
function hangManGame(word, tries = 3, responseArray = ['c', 'a', 't']) {
  const correctLetters = [];
  // getting the first from array instead of reading user input
  while (responseArray.length > 0 && tries > 0) {
    const guessedLetter = responseArray.shift()
    // make it generic instead of having to rewrite the if over and over
    // if the word contains our letter
    if (word.indexOf(guessedLetter) != -1) {
      correctLetters.push(guessedLetter)
      console.log(`correct guess: ${guessedLetter}`)
      console.log(Array.from(word).map(letter => {
        return correctLetters.some(correctLetter => correctLetter === letter) ? letter : "_"
      }))
    } else {
      console.log(`Wrong latter: ${guessedLetter} tries remaining ${--tries}`)
    }
  }
}

hangManGame('cat', 3, ['x', 'a', 'c', 'g'])

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1