Question
can you please create a javascript file code for the guess the word game that works in a diffrent file. this is the HTML page
can you please create a javascript file code for the guess the word game that works in a diffrent file.
this is the HTML page
The Word Game
hint:
This is the javascript page
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" }
];s
function initializeGame() {
// Select a random word from the array
const randomIndex = Math.floor(Math.random() * words.length);
randomWordWord = words[randomIndex].word.toLowerCase();
remainingGuesses = 10;
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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started