Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

need help reviewing my code for my game called spaceman. Need to follow the rubric and want to make sure im following everything. / /

need help reviewing my code for my game called spaceman. Need to follow the rubric and want to make sure im following everything.
// List of words to choose from
const wordList =["space", "galaxy", "astronaut", "planet"];
let guessedLetters =[];
const maxWrongGuesses =5;
let wordToGuess;
let wrongGuesses;
let displayWord;
// Function to start a new game
function newGame(){
// Choose a random word from the word list
wordToGuess = wordList[Math.floor(Math.random()* wordList.length)];
guessedLetters =[];
wrongGuesses =0;
displayWord ="_".repeat(wordToGuess.length);
gameLoop();
}
// Function to display the current state of the game
function displayGameState(){
console.log("Word: "+ displayWord.split('').join(''));
console.log("Guessed Letters: "+ guessedLetters.join(','));
console.log("Wrong guesses left: "+(maxWrongGuesses - wrongGuesses));
}
// Function to process the game loop
function gameLoop(){
while (wrongGuesses maxWrongGuesses && displayWord.includes("_")){
displayGameState();
let guess = prompt("Enter a letter to guess:").toLowerCase();
if (guessedLetters.includes(guess)){
console.log("You already guessed that letter.");
continue;
}
guessedLetters.push(guess);
if (wordToGuess.includes(guess)){
for (let i =0; i wordToGuess.length; i++){
if (wordToGuess[i]=== guess){
displayWord = displayWord.substr(0, i)+ guess + displayWord.substr(i +1);
}
}
} else {
wrongGuesses++;
}
}
if (!displayWord.includes("_")){
console.log("Congratulations! You've guessed the word: "+ wordToGuess);
} else {
console.log("Game Over! The word was: "+ wordToGuess);
}
}
// Start the game
newGame();
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Design Application Development And Administration

Authors: Mannino Michael

5th Edition

0983332401, 978-0983332404

More Books

Students also viewed these Databases questions