0

I'm trying to make a Hangman game with Javascript, where the user enters a word for another person to guess. The word is sent to a function (makeUnderlines) and is made into an array of underlines and returned to the player with a prompt to enter a letter that they want to guess. The letter and the word is sent to a function that finds the positions of the letters (getPosition) in the word and sends the positions to another function. The function (insertLetters) is supposed replace the underlines with the letter at the positions given. All of this works except that the function doesn't replace anything in the array. It only replaces the first letter if it only occurs once in the word. Any advice to solving this would be greatly appreciated. I would also like to know if there is a way to prevent the user to entering a number in a word or as a guess.

<!DOCTYPE HTML>

<HTML>
  <HEAD>
    <META CHARSET="UTF-8">
    <TITLE>Hangman</TITLE>
  </HEAD>
  <BODY>
    <SCRIPT TYPE="text/javascript">

      function makeUnderlines(word) {
        for(i = 0; i < word.length; i++){
          underlines.push("\"_\""); 
        }
        return underlines;
      }

      function getPositions(word, letter) {
        for(var i=0; i< word.length;i++) {
        if (word[i] === letter) positions.push(i);
        }
        insertLetters(underlines, positions, letter)
      }

      function insertLetters(underlines, positions, bokstav){
        underlines[positions] = letter;
        return underlines;
      }

      let word = prompt("Choose a word for the player!");
      underlines = [];
      positions = [];
      makeUnderlines(word);
      
      for(i = 7; i > 0; i--){
        letter = prompt("["+underlines+"]\n Guess a letter. You have " + i + " guesses left");
        getPositions(word, letter);
      }

    </SCRIPT>
  </BODY>
</HTML>

1 Answer 1

1

One way to start digging into this is by tracing the flow of the application with a debugger. Chrome Dev Tools has a debugger that lets you step line by line to see what the current values of your variables are.

      function insertLetters(underlines, positions, bokstav){
        underlines[positions] = letter;
        return underlines;
      }

From looking at insertLetters, positions should be an array of integer index values but it is being directly used as an integer. Maybe try iterating through the values in positions and using those to index underlines.

      function insertLetters(underlines, positions, letter){
        for (let i = 0; i < positions.length; i++) {
          const pos = positions[i];
          underlines[pos] = letter;
        }
      }

And I think the last thing you could do in getPositions is to clear out the positions array with positions = [];.

Overall your code looks quite functional. Take care to try and pass variables to functions directly instead of relying on global variables.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.