-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
167 lines (148 loc) · 4.87 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>guess a word</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="wrapper">
<h1>guess the word</h1>
<div class="content">
<input type="text" class="typing-input" maxlength="1">
<div class="inputs"> </div>
<div class="details">
<p class="hint">Hint: <span></span></p>
<p class="guess_left">remaining guesses: <span></span></p>
<p class="wrong_letters">wrong letters: <span></span></p>
</div>
<button class="reset_btn">Reset Game</button>
</div>
</div>
<script>
//storing word details as an object
const wordList = [
{
word:"search",
hint:"The act of finding something"
},
{
word:"jpeg",
hint:"a small format of an image"
},
{
word:"svg",
hint:"A vector image format"
},
{
word:"server",
hint:"related to computer or system"
},
{
word:"idea",
hint:"A thought or suggestion"
},
{
word:"act",
hint:"The doing of a thing"
},
{
word:"png",
hint:"raster type of image"
},
{
word:"silver",
hint:"a precious greyish white metal"
},
{
word:"bugs",
hint:"error in programs"
},
{
word:"avatar",
hint:"epic science fiction files"
},
];
const inputs = document.querySelector(".inputs"),
//getting random word on button click
resetbtn = document.querySelector(".reset_btn"),
//wrong letters
wrongletter = document.querySelector(".wrong_letters"),
//lets show hint of the random word
hint = document.querySelector(".hint span"),
//guessleft
guessleft = document.querySelector(".guess_left span"),
//input
typinginput = document.querySelector(".typing-input");
//making word a global variable, so that can accesss it from anywhere
//showing wrong letter here
//lets do the same for correct letters
let word , maxGuesses,corrects = [], incorrects = []; //resetting all the values to default
function randomWord(){//lets get random object from the Wordlist
let randomObj = wordList[Math.floor(Math.random() *wordList.length)];
word = randomObj.word;
//keeping it 8 by default
maxGuesses = 8; corrects = []; incorrects = [];
console.log(word);
hint.innerText = randomObj.hint;
guessleft.innerText = maxGuesses;
wrongletter.innerText = incorrects;
let html = "";
for (let i=0; i<word.length; i++){
html += ` <input type="text" disabled>`;
}
inputs.innerHTML = html;
}
//getting random object from the list
randomWord();
//getting user pressed key
function initGame(e){
let key = e.target.value;
//lets validate set pressed key if its alphabet character or number
// lets restrict the user from typing the same key twice
if(key.match(/^[A-Za-z]+$/) && !incorrects.includes(`$(key)`)
&& !corrects.includes(key) ){
console.log(key);
//lets check if entered letter is in the word or not
if(word.includes(key)){//if user letter found in the word
console.log("letter found");
//lets see the found letter in the input
for(let i = 0; i < word.length; i++){
//showing matched letter in the input value
if(word[i] === key){
corrects.push(key);
inputs.querySelectorAll("input")[i].value = key;
}
}
}else{
maxGuesses--; //decrementing guesses by 1
//lets add space between the wrong letter
incorrects.push(`${key}`);
}
guessleft.innerText = maxGuesses;
wrongletter.innerText = incorrects;
}
//lets empty the input tag once user entered the key
typinginput.value = "";
//lets show alert if user found all letters
if(corrects.length === word.length){
alert(`Congratulations!! you found the word. ${word.toUppercase()}`);
randomWord(); //calling the function so the game restart
}
else if(maxGuesses < 1){//if user id not find all thw letters
alert("Game Over!!! you do'nt have remaining guesses");
for(let i = 0; i < word.length; i++){
//show all letters in the input
inputs.querySelectorAll("input")[i].value = word[i] ;
}
}
}
resetbtn.addEventListener("click", randomWord );
typinginput.addEventListener("input", initGame)
//automatically focusing input when user press any key
document.addEventListener("keydown", () => typinginput.focus());
</script>
</body>
</html>