-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
35 lines (26 loc) · 938 Bytes
/
index.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
const firstRandomNum = Math.floor(Math.random() * 10);
const secondRandomNum = Math.floor(Math.random() * 10);
const questionEl = document.getElementById("question");
questionEl.innerText = `What is ${firstRandomNum} multiplied by ${secondRandomNum}?`;
const correctAnswer = firstRandomNum * secondRandomNum;
const formEl = document.getElementById("form");
const inputEl = document.getElementById("input");
let score = JSON.parse(localStorage.getItem("score"));
if (!score) {
score = 0;
}
formEl.addEventListener("submit", () => {
const submittedAnswer = +inputEl.value;
if (submittedAnswer == correctAnswer) {
score++;
storeInLocalStorage();
} else {
score--;
storeInLocalStorage();
}
});
function storeInLocalStorage() {
localStorage.setItem("score", JSON.stringify(score));
}
const scoreEl = document.getElementById('score')
scoreEl.innerText = `score: ${score}`