Word Guessing Game using HTML CSS and JavaScript (original) (raw)

Last Updated : 30 Jul, 2024

In this article, we will see how can we implement a word-guessing game with the help of HTML, CSS, and JavaScript.

Here, we have provided a hint key & corresponding total number of gaps/spaces depending upon the length of the word and accept only a single letter as an input for each time. If it is correct, then that letter will fill in that specific blank space, otherwise, the guessing will be continued, accordingly. It will look like the below image:

Prerequisites:

Approach:

Project Structure:

Screenshot-from-2023-07-06-12-39-41

Project Structure

**Example: This example describes the basic implementation of the **word guessing game using HTML, CSS, and Javascript.

HTML `

Word Guessing Game

Guess the word

Hint: A programming language

Guess one letter:

        <input type="text" 
               maxlength="1" 
               id="letter-input">
    </p>
    <button onclick="guessLetter()">
          Submit
      </button>
</div>

CSS

/* style.css */

/* Styling for the heading / h1 { background-color: rgb(231, 231, 231); color: green; font-size: xx-large; padding: 2%; / width: 70vh; */ }

/* Style for the card and border */ .card { min-width: 30%; width: 50vh; min-height: 30vh; box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2); transition: 0.3s; border-radius: 5px; margin: auto; padding: 2%; }

/* Font and display style for complete body */ body { display: flex; text-align: center; font-family: "Gill Sans", "Gill Sans MT", Calibri, "Trebuchet MS", sans-serif; }

/* Adding style for input box */ input { width: 30px; padding: 1%; font-size: larger; text-align: center; margin-left: 2%; }

/* Adding style for display word*/ #displayWord { font-size: xx-large; }

JavaScript

// script.js

// Array of words to choose from const words = [ "java", "javascript", "python", "pascal", "ruby", "perl", "swift", "kotlin", ];

// Geting random word from the list let randomIndex = Math.floor(Math.random() * words.length); let selectedWord = words[randomIndex]; console.log(selectedWord);

// To store the already guessed letters let guessedlist = [];

// For initial display Word let displayWord = ""; for (let i = 0; i < selectedWord.length; i++) { displayWord += "_ "; } document.getElementById("displayWord").textContent = displayWord;

// Function to check Guessed letter function guessLetter() { let inputElement = document.getElementById("letter-input");

// To check empty input
if (!inputElement.value) {
    alert("Empty Input box. Please add input letter");
    return;
}

let letter = inputElement.value.toLowerCase();

// Clear the input field
inputElement.value = "";

// Check if the letter has already been guessed
if (guessedlist.includes(letter)) {
    alert("You have already guessed that letter!");
    return;
}

// Add the letter to the guessed letters array
guessedlist.push(letter);

// Update the word display based on the guessed letters
let updatedDisplay = "";
let allLettersGuessed = true;
for (let i = 0; i < selectedWord.length; i++) {
    if (guessedlist.includes(selectedWord[i])) {
        updatedDisplay += selectedWord[i] + " ";
    } else {
        updatedDisplay += "_ ";
        allLettersGuessed = false;
    }
}
document.getElementById("displayWord")
    .textContent = updatedDisplay;

// Check if all letters have been guessed
if (allLettersGuessed) {
    alert("Congratulations! You guessed the word correctly!");
}

}

`

**Output:

Peek-2023-07-07-09-48