-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript2.js
112 lines (103 loc) · 3.5 KB
/
script2.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
const RANDOM_QUOTE_API_URL = "http://api.quotable.io/random";
const quoteDisplayElement = document.getElementById("quoteDisplay");
const quoteInputElement = document.getElementById("quoteInput");
const timerElement = document.getElementById("timer");
const endgameEl = document.getElementById("end-game-container");
let previousCorrectWordCount = 0;
let correctWordsCount = 0;
let previousCharCount = 0;
let totalCharCount = 0;
let startTime = 31;
let wrongCharCount = 0; // initialize to 0
let timer1;
quoteInputElement.addEventListener("input", () => {
const arrayQuote = quoteDisplayElement.querySelectorAll("span");
const arrayValue = quoteInputElement.value.split("");
let correct = true;
arrayQuote.forEach((characterSpan, index) => {
const character = arrayValue[index];
if (character == null) {
characterSpan.classList.remove("correct");
characterSpan.classList.remove("incorrect");
correct = false;
} else if (character === characterSpan.innerText) {
characterSpan.classList.add("correct");
characterSpan.classList.remove("incorrect");
} else {
if (!characterSpan.classList.contains("incorrect")) {
characterSpan.classList.remove("correct");
characterSpan.classList.add("incorrect");
wrongCharCount = wrongCharCount + 1;
console.log(wrongCharCount);
}
correct = false;
}
});
totalCharCount = previousCharCount + arrayValue.length;
let mergedInnerHTML = "";
for (let i = 0; i < arrayQuote.length; i++) {
if (arrayQuote[i].classList.contains("correct")) {
mergedInnerHTML += arrayQuote[i].innerHTML;
}
}
const correctWords = mergedInnerHTML.trim().split(" ");
correctWordsCount = correctWords.length + previousCorrectWordCount;
if (correct) {
renderNewQuote();
previousCorrectWordCount = correctWordsCount;
console.log(previousCorrectWordCount);
previousCharCount = arrayValue.length;
};
});
function getRandomQuote() {
return fetch(RANDOM_QUOTE_API_URL)
.then((response) => response.json())
.then((data) => data.content);
}
async function renderNewQuote() {
const quote = await getRandomQuote();
quoteDisplayElement.innerHTML = "";
quote.split("").forEach((character) => {
const characterSpan = document.createElement("span");
characterSpan.innerText = character;
quoteDisplayElement.appendChild(characterSpan);
});
quoteInputElement.value = null;
}
function startTimer() {
timer1 = new Date();
setInterval(() => {
const remainingTime = getTimerTime();
if (remainingTime >= 0) {
timerElement.innerText = remainingTime;
} else {
clearInterval(timer1);
gameOver();
}
}, 1000);
}
console.log(wrongCharCount);
function gameOver() {
console.log(totalCharCount);
console.log(wrongCharCount);
let accuracy = (((totalCharCount - wrongCharCount) / totalCharCount) * 100).toFixed(2);
endgameEl.innerHTML = `
<h1 class="ranOut">Time ran out!</h1>
<div class="flex">
<p>Accuracy: ${accuracy}% </p>
<p>Speed: ${(correctWordsCount/(startTime/60)).toFixed(1)} WPM</p>
</div>
<button class="buttonReload" onclick="location.reload()">Play Again</button>
`;
endgameEl.style.display = "flex";
timerElement.style.display = "none";
endgameEl.style.fontSize = "30px";
endgameEl.style.borderRadius = "20px";
}
function getTimerTime() {
const elapsedTime = Math.floor((new Date() - timer1) / 1000);
const remainingTime = startTime - elapsedTime;
return remainingTime;
}
renderNewQuote();
startTimer();