-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
108 lines (89 loc) · 3.09 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
import { words } from "./fiveLetterWords.js";
const howToPlayBtn = document.querySelector(".how-to-play button");
const howToPlayContent = document.querySelector(".howtoplay-content-container");
const input = document.querySelector("input");
const invalidAlert = document.querySelector(".invalidword-alert");
const answersDiv = document.querySelector(".answer-grid");
const answerCorrect = document.querySelector(".success-container");
const answerWrong = document.querySelector(".fail-container");
const solutionSpan = document.querySelector(".fail span");
const answers = ["", "", "", "", "", ""];
const solution = words[Math.floor(Math.random() * words.length)];
let attemptNumber = 0;
input.addEventListener("change", submitAnswer);
howToPlayBtn.addEventListener("click", () => {
howToPlayContent.classList.add("display");
});
howToPlayContent.addEventListener("click", () => {
howToPlayContent.classList.remove("display");
});
answerCorrect.addEventListener("click", () => {
answerCorrect.classList.remove("display");
});
answerWrong.addEventListener("click", () => {
answerWrong.classList.remove("display");
});
function submitAnswer(e) {
const answer = e.target.value.toLowerCase();
input.classList.remove("invalid-answer");
if (answer.length !== 5) {
input.classList.add("invalid-answer");
invalidAlert.innerHTML = "Answer must have 5 letters";
return;
}
if (!words.includes(answer)) {
input.classList.add("invalid-answer");
invalidAlert.innerHTML = "Answer is not in the word list";
return;
}
answers[attemptNumber] = answer;
attemptNumber++;
updateHtml();
colourAnswers();
input.value = "";
if (answer === solution) {
answerCorrect.classList.add("display");
input.removeEventListener("change", submitAnswer);
return;
}
if (attemptNumber === 5) {
solutionSpan.innerHTML = solution;
answerWrong.classList.add("display");
input.removeEventListener("change", submitAnswer);
}
}
function updateHtml() {
const htmlContent = answers
.map(
(answer) =>
`<div class='answer'>${answer
.split("")
.map((letter) => `<span class='letter-span'>${letter}</span>`)
.join("")}</div>`
)
.join("");
answersDiv.innerHTML = htmlContent;
}
function colourAnswers() {
const answerRows = document.querySelectorAll(".answer");
answers.forEach((answer, rowIndex) => {
const answerRow = answerRows[rowIndex];
const answerSpans = answerRow.querySelectorAll(".letter-span");
const solutionLetters = solution.split("");
const answerLetters = answer.split("");
answerLetters.forEach((letter, index) => {
if (solutionLetters[index] === letter) {
answerSpans[index].classList.add("green");
answerLetters.splice(index, 1, "");
solutionLetters.splice(index, 1, "");
}
});
answerLetters.forEach((letter, index) => {
if (solutionLetters.includes(letter)) {
answerSpans[index].classList.add("yellow");
const indexInSolution = solutionLetters.indexOf(letter);
solutionLetters.splice(indexInSolution, 1, "");
}
});
});
}