-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
38 lines (28 loc) · 893 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
36
const num1 = Math.ceil(Math.random() * 10);
const num2 = Math.ceil(Math.random() * 10);
const questionnEl = document.getElementById("question");
const formEl = document.getElementById("form");
const inputEl = document.getElementById("input");
const btn = document.getElementById("btn_submit");
const scoreEl = document.getElementById("score");
questionnEl.innerText = `What is ${num1} multiply by ${num2}?`;
const correctAns = num1 * num2;
console.log(correctAns);
let score = JSON.parse(localStorage.getItem("score"));
if (!score) {
score = 0;
}
scoreEl.innerText = `score: ${score}`;
formEl.addEventListener("submit", () => {
const userAns = +inputEl.value;
if (userAns === correctAns) {
score++;
updateLocalStorage();
} else {
score--;
updateLocalStorage();
}
});
function updateLocalStorage() {
localStorage.setItem("score", JSON.stringify(score));
}