-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
143 lines (128 loc) · 4.12 KB
/
script.js
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
//Word and Hints Object
const options = {
aroma: "Pleasing smell",
pepper: "Salt's partner",
halt: "put a stop to",
jump: "Rise suddenly ",
shuffle: "Mix cards up ",
combine: "Add; Mix",
chaos: "Total disorder",
labyrinth: "Maze",
disturb: "Interrupt; upset ",
shift: "Move; Period of word",
machine: "Device or appliance",
};
//Initial References
const message = document.getElementById("message");
const hintRef = document.querySelector(".hint-ref");
const controls = document.querySelector(".controls-container");
const startBtn = document.getElementById("start");
const letterContainer = document.getElementById("letter-container");
const userInpSection = document.getElementById("user-input-section");
const resultText = document.getElementById("result");
const word = document.getElementById("word");
const words = Object.keys(options);
let randomWord = "",
randomHint = "";
let winCount = 0,
lossCount = 0;
//Generate random value
const generateRandomValue = (array) => Math.floor(Math.random() * array.length);
//Block all the buttons
const blocker = () => {
let lettersButtons = document.querySelectorAll(".letters");
stopGame();
};
//Start Game
startBtn.addEventListener("click", () => {
controls.classList.add("hide");
init();
});
//Stop Game
const stopGame = () => {
controls.classList.remove("hide");
};
//Generate Word Function
const generateWord = () => {
letterContainer.classList.remove("hide");
userInpSection.innerText = "";
randomWord = words[generateRandomValue(words)];
randomHint = options[randomWord];
hintRef.innerHTML = `<div id="wordHint">
<span>Hint: </span>${randomHint}</div>`;
let displayItem = "";
randomWord.split("").forEach((value) => {
displayItem += '<span class="inputSpace">_ </span>';
});
//Display each element as span
userInpSection.innerHTML = displayItem;
userInpSection.innerHTML += `<div id='chanceCount'>Chances Left: ${lossCount}</div>`;
};
//Initial Function
const init = () => {
winCount = 0;
lossCount = 5;
randomWord = "";
word.innerText = "";
randomHint = "";
message.innerText = "";
userInpSection.innerHTML = "";
letterContainer.classList.add("hide");
letterContainer.innerHTML = "";
generateWord();
//For creating letter buttons
for (let i = 65; i < 91; i++) {
let button = document.createElement("button");
button.classList.add("letters");
//Number to ASCII[A-Z]
button.innerText = String.fromCharCode(i);
//Character button onclick
button.addEventListener("click", () => {
message.innerText = `Correct Letter`;
message.style.color = "#008000";
let charArray = randomWord.toUpperCase().split("");
let inputSpace = document.getElementsByClassName("inputSpace");
//If array contains clicked value replace the matched Dash with Letter
if (charArray.includes(button.innerText)) {
charArray.forEach((char, index) => {
//If character in array is same as clicked button
if (char === button.innerText) {
button.classList.add("correct");
//Replace dash with letter
inputSpace[index].innerText = char;
//increment counter
winCount += 1;
//If winCount equals word length
if (winCount == charArray.length) {
resultText.innerHTML = "You Won";
startBtn.innerText = "Restart";
//block all buttons
blocker();
}
}
});
} else {
//lose count
button.classList.add("incorrect");
lossCount -= 1;
document.getElementById(
"chanceCount"
).innerText = `Chances Left: ${lossCount}`;
message.innerText = `Incorrect Letter`;
message.style.color = "#ff0000";
if (lossCount == 0) {
word.innerHTML = `The word was: <span>${randomWord}</span>`;
resultText.innerHTML = "Game Over";
blocker();
}
}
//Disable clicked buttons
button.disabled = true;
});
//Append generated buttons to the letters container
letterContainer.appendChild(button);
}
};
window.onload = () => {
init();
};