Question

Simple JavaScript and HTML: You'll create a simple word guessing game where the user gets infinite...

Simple JavaScript and HTML:

You'll create a simple word guessing game where the user gets infinite tries to guess the word (like Hangman without the hangman, or like Wheel of Fortune without the wheel and fortune).

Create two global arrays: one to hold the letters of the word (e.g. 'F', 'O', 'X'), and one to hold the current guessed letters (e.g. it would start with '_', '_', '_' and end with 'F', 'O', 'X').

Write a function called guessLetter that will:

  • Take one argument, the guessed letter.

  • Iterate through the word letters and see if the guessed letter is in there.

  • If the guessed letter matches a word letter, changed the guessed letters array to reflect that.

  • When it's done iterating, it should log the current guessed letters ('F__')

  • and congratulate the user if they found a new letter.

  • It should also figure out if there are any more letters that need to be guessed.

  • and if not, it should congratulate the user for winning the game.

Pretend you don't know the word, and call guessLetter multiple times with various letters to check that your program works.

Paste ALL the codes here (.js file and .html file) and also show a screenshot of the code working and running.

I will Up Vote!

0 0
Add a comment Improve this question Transcribed image text
Answer #1

CODE:

index.html

<html lang="en">

  <head>

    <meta charset="UTF-8" />

    <meta name="viewport" content="width=device-width, initial-scale=1.0" />

    <title>Document</title>

  </head>

  <body>

    <h1>Guess the Word</h1>

    <div id="mydiv"><h2 id="guessedWord">_ _ _ _ _</h2></div>

    <input type="text" placeholder="Enter a character to guess" id="str" />

    <button onclick="clicked();">Guess</button>

    <h3 id="message">Keep Playing</h3>

  </body>

  <script src="script.js"></script>

</html>

script.js

var word = ["a", "e", "r", "o", "p", "l", "a", "n", "e"];

var guess = ["_", "_", "_", "_", "_", "_", "_", "_", "_"];

const guessLetter = str => {

  let flag = 0;

  // traversing the whole word array to check if guessed char is equal to it or not

  for (var i = 0; i < word.length; i++) {

    if (word[i] === str) {

      flag = 1;

      guess[i] = str;

    }

  }

  console.log(guess.join(" "));

  // if the guess letter was correct

  if (flag == 1) {

    document.getElementById("guessedWord").innerHTML = guess.join(" ");

    document.getElementById("str").value = "";

    document.getElementById("message").innerHTML =

      "Congratulation you guessed the right letter";

    // console.log("Congratulation you guessed the right letter");

  }

  // if all the letters are guessed

  if (!guess.includes("_")) {

    document.getElementById("message").innerHTML =

      "Congratulation you won the game";

    // console.log("Congratulation you won the game");

  }

};

const clicked = () => {

  const str = document.getElementById("str").value;

  guessLetter(str);

};

OUTPUT:

Guess the Word ----- Enter a character to guess Guess Keep Playing

Guess the Word a e_--_a_e Enter a character to guess Guess Congratulation you guessed the right letter

Guess the Word a eroplane Enter a character to guess Guess Congratulation you won the game

Console log

script.js:13 script.js:13 script.js:13 script.js:13 script.js:13 script.js:13 script.js:13 aeroplane

Please upvote if you like my answer and comment below if you have any queries.

Add a comment
Know the answer?
Add Answer to:
Simple JavaScript and HTML: You'll create a simple word guessing game where the user gets infinite...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Create a script that presents a word-guessing game. Allow users to guess the word one letter at a...

    Create a script that presents a word-guessing game. Allow users to guess the word one letter at a time by entering a character in a form. Start by assigning a secret word to a variable. After each guess, print the word using asterisks for each remaining letter, but fill in the letters that the user guessed correctly. Store the user’s guess in a form field. For example, if the word you want users to guess is “suspicious” and the user...

  • Create a script that presents a word guessing game. Allow users to guess the word letter-by-letter by entering a character in a form. Start by assigning a secret word to a variable named $secret. Afte...

    Create a script that presents a word guessing game. Allow users to guess the word letter-by-letter by entering a character in a form. Start by assigning a secret word to a variable named $secret. After each guess, print the word using asterisks for each remaining letter but fill in the letters that the user guessed correctly. You need to store the user’s guess in a hidden text field name $hidden_guess. For example, if the word you want users to guess...

  • Please create a Hangman game in Java language. For this game a random word will be...

    Please create a Hangman game in Java language. For this game a random word will be selected from the following list (abstract, cemetery, nurse, pharmacy, climbing). You should keep a running tab of the user’s score and display it on screen; the user begins with 100 pts possible if they correctly guess the word without any mistakes. For every incorrect letter which is guessed, 10 pts should be taken away from what is left of their total possible points. For...

  • Create the game hangman using c code: Program description In the game of hangman, one player...

    Create the game hangman using c code: Program description In the game of hangman, one player picks a word, and the other player has to try to guess what the word is by selecting one letter at a time. If they select a correct letter, all occurrences of the letter are shown. If no letter shows up, they use up one of their turns. The player is allowed to use no more than 10 incorrect turns to guess what the...

  • Please help with this Intro to programming in C assignment! Intro to Programming in C-Large Program...

    Please help with this Intro to programming in C assignment! Intro to Programming in C-Large Program 3 - Hangman Game Assignment purpose: User defined functions, character arrays, c style string member functions Write an interactive program that will allow a user to play the game of Hangman. You will need to: e You will use four character arrays: o one for the word to be guessed (solution) o one for the word in progress (starword) o one for all of...

  • Create a modification to the Hangman game to provide enhancements for the user experience. Share the...

    Create a modification to the Hangman game to provide enhancements for the user experience. Share the code in text format in this thread. Could someone please assist me coming up with a modification in Python? 4. EXERCISE 4 The following code implements a very simple Hangman game: word = 'underutilise guesses = [] user_input = " while user_input == '0': user_input = input ("Enter a letter, or 0 to give up:') guesses.append(user_input) output = '' for letter in range(1, len...

  • Create a new program, WordGuessingGame. Using a while loop, create a game for the user. The...

    Create a new program, WordGuessingGame. Using a while loop, create a game for the user. The game requires the user to guess a secret word. The game does not end until the user guesses the word. After they win the game, they are prompted to choose to play again. The secret word that user must guess is "valentine". It is a case-sensitive analysis With each guess... If the guess does not have an equal number of characters, tell the user...

  • This is for C programming: You will be given files in the following format: n word1...

    This is for C programming: You will be given files in the following format: n word1 word2 word3 word4 The first line of the file will be a single integer value n. The integer n will denote the number of words contained within the file. Use this number to initialize an array. Each word will then appear on the next n lines. You can assume that no word is longer than 30 characters. The game will use these words as...

  • can this code be provided? Project #3 Introduction As you wrap up with JavaScript, let's put...

    can this code be provided? Project #3 Introduction As you wrap up with JavaScript, let's put together everything you've learned to make the classic game "Hangman" in a webpage. If you are unfamiliar with the rules of Hangman, the game works like this: A gallows is drawn on a surface, and Player 1 chooses a word that player 2 must guess. Empty dashes for each letter in the word are drawn next to the gallows (so a 7-letter word means...

  • For a C program hangman game: Create the function int play_game [play_game ( Game *g )]...

    For a C program hangman game: Create the function int play_game [play_game ( Game *g )] for a C program hangman game. (The existing code for other functions and the program is below, along with what the function needs to do) (Also the link to program files (hangman.h and library file) is below the existing code section. You can use that to check if the code works) What int play_game needs to do mostly involves calling other functions you've already...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT