-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
154 lines (138 loc) · 5.18 KB
/
index.js
File metadata and controls
154 lines (138 loc) · 5.18 KB
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
let correctWord = [];
let wordArray = [];
let guessedLetters = [];
const letterArray = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"];
const green = "#81B622";
const grey = "#909090";
let hangmanNumber = 1;
const difficulty = {
easy: 1,
medium: 2,
hard: 3
};
document.addEventListener("DOMContentLoaded", () => {
difficultyOnClick();
});
/**
*
* @param arr an Array of Strings
* @returns an array of characters created from one of the argument's elements
*/
function randomWord(arr) {
return arr[Math.floor(Math.random() * wordArray.length)].toUpperCase().split("");
}
function onClickStuff() {
let wordTable = document.querySelector("#wordTable");
let restartButton = document.querySelector("#restartButton");
correctWord.forEach((letter) => {
let letterSpace = document.createElement("th");
letterSpace.className = "wordClass";
letterSpace.id = letter + "Space";
letterSpace.style.fontFamily = "Georgia, 'Times New Roman', Times, serif";
letterSpace.textContent = "_";
wordTable.appendChild(letterSpace);
console.log(letter);
});
letterArray.forEach((letter) => {
let letterCell = document.querySelector("#" + letter);
let hangmanPic = document.querySelector("#hangmanPic");
letterCell.addEventListener("click", () => {
if (!guessedLetters.includes(letter.toUpperCase())) {
guessedLetters.push(letter.toUpperCase());
letterCell.style.backgroundColor = grey;
if (correctWord.includes(letter.toUpperCase())) {
correctWord.forEach((element) => {
updateCorrectWord(element, letter);
});
}
else {
hangmanPic.src = "images/" + hangmanNumber + ".jpg";
hangmanNumber++;
}
if (isGameWon()) {
//alert("YOU WIN!\nANSWER: " + correctWord.join(""));
runWinScreen();
}
if (isGameLost()) {
//alert("YOU LOSE!\nANSWER: " + correctWord.join(""));
runLoseScreen();
}
}
});
});
restartButton.addEventListener("click", () => {
location.reload();
});
}
function updateCorrectWord(element, letter) {
if (element === letter.toUpperCase()) {
let wordCellGroup = document.querySelectorAll("#" + element.toUpperCase() + "Space");
for (wordCell of wordCellGroup) {
wordCell.textContent = letter.toUpperCase();
}
}
}
function isGameLost() {
let hangmanImg = document.querySelector("#hangmanPic").src;
return hangmanImg.slice(hangmanImg.length - 13) === "/images/6.jpg";
}
function isGameWon() {
return correctWord.every(element => guessedLetters.includes(element));
}
function runWinScreen() {
let gameBox = document.querySelector("#gameBox");
let overBox = document.querySelector("#overBox");
let gameOverHeader = document.querySelector("#gameOverHeader");
let correctWordText = document.querySelector("#correctAnswer");
gameOverHeader.textContent = "YOU WIN!";
gameBox.style.display = "none";
overBox.style.display = "block";
correctWordText.textContent = correctWord.join("");
}
function runLoseScreen() {
let gameBox = document.querySelector("#gameBox");
let overBox = document.querySelector("#overBox");
let gameOverHeader = document.querySelector("#gameOverHeader");
let correctWordText = document.querySelector("#correctAnswer");
gameOverHeader.textContent = "YOU LOSE!"
gameBox.style.display = "none";
overBox.style.display = "block";
correctWordText.textContent = correctWord.join("");
}
function difficultyOnClick() {
let easyButton = document.querySelector("#easyButton");
let mediumButton = document.querySelector("#mediumButton");
let hardButton = document.querySelector("#hardButton");
easyButton.addEventListener("click", () => {loadWords(difficulty.easy)})
mediumButton.addEventListener("click", () => {loadWords(difficulty.medium)});
hardButton.addEventListener("click", () => {loadWords(difficulty.hard)});
}
function loadWords(num) {
fetch("http://localhost:3000/wordArray")
.then(res => res.json())
.then(js => {
wordArray = js.filter((element) => {
switch(num){
case 1:
if(element.length <= 5) {
return element;
}
break;
case 2:
if(element.length > 5 && element.length <= 9) {
return element;
}
break;
case 3:
if(element.length > 9) {
return element;
}
break;
}
});
correctWord = randomWord(wordArray);
onClickStuff();
document.querySelector("#startBox").style.display = "hidden";
document.querySelector("#gameBox").style.display = "block";
});
}