Skip to content

Commit

Permalink
Merge pull request #1049 from Alisha-786/master
Browse files Browse the repository at this point in the history
Created a Word Guessing Game
  • Loading branch information
NitkarshChourasia authored Jan 12, 2024
2 parents 785ae9f + ed102b5 commit abac7ea
Showing 1 changed file with 98 additions and 0 deletions.
98 changes: 98 additions & 0 deletions Word Guess Game/Alisha-786/word_guess.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<!DOCTYPE html>
<html>
<head>
<title>Word Guessing Game</title>
<style>
body {
font-family: Arial, sans-serif;
text-align: center;
}

#word {
font-size: 24px;
font-weight: bold;
margin-top: 20px;
}

#letters {
font-size: 20px;
margin-top: 10px;
}

#message {
font-size: 18px;
margin-top: 10px;
color: green;
}
</style>
</head>
<body>
<h1>Word Guessing Game</h1>
<div id="word">_ _ _ _ _</div>
<div id="letters">Letters Guessed: </div>
<input type="text" id="guess" placeholder="Enter a letter">
<button onclick="guessLetter()">Guess</button>
<div id="message"></div>

<script>
const words = ["apple", "banana", "cherry", "date", "elderberry", "fig"];
let selectedWord = words[Math.floor(Math.random() * words.length)];
let guessedWord = Array(selectedWord.length).fill("_");
let guessedLetters = [];
let attempts = 6;

function displayWord() {
document.getElementById("word").textContent = guessedWord.join(" ");
}

function displayLetters() {
document.getElementById("letters").textContent =
"Letters Guessed: " + guessedLetters.join(", ");
}

function displayMessage(message) {
document.getElementById("message").textContent = message;
}

function guessLetter() {
let guess = document.getElementById("guess").value.toLowerCase();
if (guess.length !== 1 || !/[a-z]/.test(guess)) {
alert("Please enter a single letter.");
return;
}

if (guessedLetters.includes(guess)) {
alert("You already guessed that letter.");
return;
}

guessedLetters.push(guess);
displayLetters();

let found = false;
for (let i = 0; i < selectedWord.length; i++) {
if (selectedWord[i] === guess) {
guessedWord[i] = guess;
found = true;
}
}

if (!found) {
attempts--;
}

displayWord();

if (guessedWord.join("") === selectedWord) {
displayMessage("Congratulations! You've won!");
document.getElementById("guess").disabled = true;
} else if (attempts === 0) {
displayMessage("Sorry, you've run out of attempts. The word was: " + selectedWord);
document.getElementById("guess").disabled = true;
}
}

displayWord();
</script>
</body>
</html>

0 comments on commit abac7ea

Please sign in to comment.