Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

A word guess game in javascript: Find some random words in different length. The

ID: 3780318 • Letter: A

Question

A word guess game in javascript:

Find some random words in different length. The length should vary from 3 to 20 ( at least). Store them into array(s).

Have a button to start the game

For each round, the letters in a word will be displayed, however in random sequence.

Provide a text box for user to type in the guess for the word. A submit button to submit the guess. User has three chances.

If user guess the correct word, then go to next round. The length of the word should increase in next round. For example, if the length in first round is 3, then the next round, the webpage should use a word with at least 4 letters.

Calculate user scores. Scores for each round can be calculated by using (Word Length) / (Number of Tries) * 10

If user used up all the three chances in one round, then the game is over. Display the total score for the user.

Name your game as XXX’s Word Guess Game. XXX is your last name.

Each time user guess out the correct word, user can collect stars. Less guess will get more stars. Your webpage can display an animation for the stars to appear. Display the total stars user has collected. (20 points)

Add sound effect for the guess and collection of stars. (10 points)

Explanation / Answer

The code allows first user to enter any word and second user to guess it.

// Guess the word one letter at a time.
// Each player is only allowed to guess
// wrong three times.

// Create a variable to store the number of bad guesses
var bad_guesses = 0;
// Create another array to store good guesses
var good_guesses = [];

// Prompt Player 1 to enter a word to guess and store
// as a variable.
// TODO: put this in a loop until a valid response is received.
var secret_word = prompt("Player 1: Enter your secret word.");

// Fill this array with placeholders for guessing
for (i = 0; i < secret_word.length; i++) {
good_guesses.push("_");
}

// Start a loop that continues as long as the person has
// not guessed wrong three times, or all of the letters have
// been guessed.
while (bad_guesses !== 3 && good_guesses.indexOf("_") !== -1) {

// Prompt Player 2 to guess a letter and store as
// a variable.
// TODO: put this in a loop until a valid response is received.
var guess = prompt(good_guesses.join(" ") + " " + "Player 2: Guess a letter.");

// If the letter does not exist in the word,
// add it to the bad guesses.
if (secret_word.indexOf(guess) === -1) {
bad_guesses++;
alert("Bad guess!");

// If the letter exists in the word, we need to
// add it to the good guesses array
} else {
for (i = 0; i < secret_word.length; i++) {
// Each time the guess letter is found, we
// add it as a good guess in the same spot
if (secret_word[i] === guess) {
good_guesses[i] = guess;
}
}
}
}

// Once the player has exited the loop, congratulate
// them on a win, or tell them they have lost and show
// the secret word.
if (bad_guesses === 3) {
alert("Sorry, please play again!");
} else {
alert("Congratulations on your win!");
}
alert("The secret word was " + secret_word);

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote