Question: can you make this javascript code more simple ? // Array of words and hint const words = [ { word: apple, hint: A red
can you make this javascript code more simple ?
// Array of words and hint const words = [ { word: "apple", hint: "A red fruit" }, { word: "car", hint: "a vehicle with four wheels" }, { word: "zebra", hint: "an animal with four legs and a tail " }, { word: "lion", hint: "an animal with mane" }, { word: "pizza", hint: "a food in a triangle slice" }, { word: "banana", hint: "A yellow fruit" }, { word: "blueberries", hint: "a type of fruit" }, { word: "Blackberry", hint: "a type of fruit" }, { word: "laptops", hint: "An electronic device" }, { word: "computer", hint: "An electronic device" }, { word: "cellphones", hint: "An electronic device" }, { word: "watermelon", hint: "a type of fruit" }, { word: "bed", hint: "an object you sleep on" }, { word: "clothes", hint: "something you wear" }, { word: "water", hint: "somehthing you drink" } ];
function initializeGame() { // Select a random word from the array const randomIndex = Math.floor(Math.random() * words.length); randomWordWord = words[randomIndex].word.toLowerCase(); remainingGuesses = 10; // To store the guessed letters guessedLetters = []; correctLetters = []; displayWord(); }
// placeholders and hint function displayWord() { const wordContainer = document.getElementById('word-container'); const hintContainer = document.getElementById('hint-container');
wordContainer.innerHTML = ''; hintContainer.innerHTML = 'Hint: ' + words.find(wordObj => wordObj.word === randomWord).hint;
for (const letter of selectedWord) { if (correctLetters.includes(letter)) { wordContainer.innerHTML += letter + ' '; } else { wordContainer.innerHTML += '_ '; } } }
// incorrect letters function displayIncorrectLetters() { const incorrectLettersContainer = document.getElementById('incorrect-letters'); incorrectLettersContainer.textContent = 'Incorrect Letters: ' + guessedLetters.join(', '); }
// Check if the letters are correct function checkGuess(letter) { if (selectedWord.includes(letter)) { // Correct correctLetters.push(letter); } else { // Incorrect guessedLetters.push(letter); remainingGuesses--; } displayWord(); displayIncorrectLetters(); checkGameStatus(); }
// Check if the game is won or lost function checkGameStatus() { const wordContainer = document.getElementById('word-container'); const guessesLeftSpan = document.getElementById('guesses-left');
if (correctLetters.length === randomWord.length) { // Player wins wordContainer.innerHTML = 'Congratulations! You guessed the correct word!'; } else if (remainingGuesses === 0) { // Player loses wordContainer.innerHTML = 'Game over! The correct word was: ' + randomWord + ''; }
guessesLeftSpan.textContent = remainingGuesses;
if (remainingGuesses === 0 || correctLetters.length === randomWordWord.length) { // if the game is over document.getElementById('guess-input').disabled = true; } } let randomWord, remainingGuesses, guessedLetters, correctLetters;
// Function for user guesses function guess() { const inputElement = document.getElementById('guess-input'); const guess = inputElement.value.toLowerCase();
if (guess && guessedLetters.indexOf(guess) === -1 && !correctLetters.includes(guess)) { checkGuess(guess); }
inputElement.value = ''; // Clear }
window.onload = initializeGame;
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
