Javascript Hangman

Hi I am trying to create a hangman game purely out of javascript, I am currently having an error where the hangman does not end even after the user has tried to answer the correct alphabet for a single word 9 times.
The goal of the game is to allow the user to guess up to 10 words , with the condition that they do not get any of the words wrong. E.g 1 word 7 attempts to enter the correct alphabet, if all 7 attempts are up then the game ends. If the user gets one word correct it moves on to the next word. The words are imported from a csv file. Thanks for helping out !

 

var readline = require("readline-sync");

class Word {
constructor(word, definition){
 this.word = word
 this.definition = definition
}
}

class WordCollection {
constructor(pool, words){
 this.pool = [];
 this.readFile();
}
readFile() {
 const filePath = 'C:/FoP';
 const filename = filePath + '/input.csv';
 const fs = require('fs');



 try {
     const text =fs.readFileSync(filename, "UTF-8");

     const textByLine = text.split('\r\n');
     var wordline;

     for (var i = 0 ; (i < textByLine.length) ; i++) {
         wordline = textByLine[i].split(',');
         this.pool.push(new Word(wordline[0],wordline[1]));
     }
 } catch(err) {;
     console.log('file ' + " " + filename + " " + 'not found. Program terminated');
     process.exit();
 }        
}
}
console.clear();


var hangmanPics = [
    ` 
       _____
      |     |
      |
      |
      |
      |
     _|_
    |   |_____
    |         |
    |_________|`, 
   
    ` 
       _____
      |     |
      |     o
      |
      |
      |
     _|_
    |   |_____
    |         |
    |_________|`, 
   
    ` 
       _____
      |     |
      |     o
      |     |
      |
      |
     _|_
    |   |_____
    |         |
    |_________|`, 
   
    ` 
       _____
      |     |
      |     o
      |    /|
      |
      |
     _|_
    |   |_____
    |         |
    |_________|`, 
   
    ` 
       _____
      |     |
      |     o
      |    /|\\
      |
      |
     _|_
    |   |_____
    |         |
    |_________|`, 
   
    ` 
       _____
      |     |
      |     o
      |    /|\\
      |     |
      |
     _|_
    |   |_____
    |         |
    |_________|`, 
   
    ` 
       _____
      |     |
      |     o
      |    /|\\
      |     |
      |    /
     _|_
    |   |_____
    |         |
    |_________|`,
  
    ` 
       _____
      |     |
      |     o
      |    /|\\
      |     |
      |    / \\
     _|_
    |   |_____
    |         |
    |_________|`]

console.log("\n" + "-= Welcome to HangMan =-")
var namein = readline.question("\n" + "Please Enter Your Name :")

var game = new WordCollection();
var wordrandom = game.pool[Math.floor(Math.random() * game.pool.length)];
 

var outputchosenword = wordrandom.word.toLowerCase();
console.log(outputchosenword)

var Alphabet = ['\nA', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
 'I', 'J', 'K', 'L', 'M','\nN', 'O', 'P', 'Q', 'R', 'S',
 'T', 'U', 'V', 'W', 'X', 'Y', 'Z\n'];
console.log(Alphabet.join(' '))


var attempts = 0;
var life = 9;
    
    //Make array for answer
    var answer = [];
        for (var i = 0; i < outputchosenword.length; i++) {
            answer[i] = "_";
        }
    
    var remainingLetters = outputchosenword.length;
        
        //The game loop
        while (remainingLetters > 0  ) {
            //Showing the letters in form of "_"
            console.log("This is the word to guess:\n" + answer.join(" "));
            
            //Prompt player to guess
            var guess = readline.question(namein + "'s guess (Enter 9 for lifelines or 0 to pass): ")
            if (guess === null) {
                 //Leave the game
                break;
            } else if (guess.length !== 1) {
                console.log("Please enter one single letter.");
            } else {
                //Update match with guess
                var goodGuess = false;
                for (var j = 0; j < outputchosenword.length; j++) {
                if (outputchosenword[j] === guess) {
                var no = 0;
                console.log("Good Job ! You have found " + guess)

                 goodGuess = true;

            // This is to check if the user has entered the same alphabet if so alert the user
            if (answer[j] !== "_") {
            console.log("Letter already be guessed");
            break;
           } else {

            answer[j] = guess;
            remainingLetters--;
            }
          }
        }   
     }
        
       if(!goodGuess){
        console.log("Sorry, " + guess + " is not a part of the word ");
        attempts++
         
        console.log(hangmanPics[attempts]);
        --life 
        
        } 
    }
              
     //End of playing loop

        
        
        //Show answer and congratulate the player
        console.log(answer.join(" "));
        console.log("Good work! The right answer is " + outputchosenword);



Hey there,

nice to meet you!

Would be awesome if you could create a project on codesandbox.io so when can fiddle around with your code.

Looking forward to having a look at it!

I can’t tell with a quick look, but do you have any variables or counters that need to be reset after you check the inputs?

Hi I have moved the code to sandbox, but I am not sure if it is able to run as my goal is to purely run it on javascript with visual studios.

Currently I managed to get the game to work but I would like the game to lose when the user has attempted to answer the correct alphabet for a single word 9 times, may I know how I can do that ?

Have you tried to map out the algorithm you would need to accomplish this?

You could declare a counter onload and increment it whenever the user guesses. If the counter goes over, you can then trigger the game over function.

I have tried doing that but it seems like the game keeps on running
, I placed a counter where if the user guesses a letter incorrectly an increment of +1 would be triggered . and when that value hits lets say 7. the game would end

Do you have a way for the program to read the counter each time?

would I need to create a function in order to do that ? I am currently stuck when the user solves a word and the game moves on to another word.

I would definitely try to simplify the game so that the basic gameplay works, then add in the extra cool stuff like Node and outside files and error handling. Because I think the problem is in the basic behavior of the game. You could comment all that stuff out and use a constant, maybe a single word until things are working… then add the other things.

I would also try to structure the game so that it’s more modular. You have a runGame() function, inside the runGame() function is the while loop. The while loop might call a playTurn() function, where the user makes their guesses. At the end of each playTurn, you increment the counter and you check the counter, maybe with an if statement. So if during the playTurn, the player wins, then you can have it call a reset() function.

The reset function sets out the variables to their starting state and, and the end of the reset funtion, starts a new game. Like:

function reset() {
attempts = 0;
remaining letters = 0;
runGame();
}

But this is just personal preference. The main thing is you look at right what happens after the game ends and compare that state with the state right when you load it up. What do you need to do to get back to the starting state? Whatever it is, just make sure you tell the program to do that right there.

Hope this is somehow helpful.