-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
40 lines (27 loc) · 869 Bytes
/
Copy pathmain.js
File metadata and controls
40 lines (27 loc) · 869 Bytes
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
//generate a random number from 1 --> 10 without decimals
const Num1 = Math.ceil(Math.random()*10);
const Num2 = Math.ceil(Math.random()*10);
const question = document.getElementById("question");
const formEl = document.getElementById("form");
const inputEl = document.getElementById("input");
const scoreEl = document.getElementById("score");
let score = JSON.parse(localStorage.getItem("score"));
// if (!score){
// score=0;
// }
scoreEl.innerHTML=`Score: ${score}`;
question.innerText = `What is ${Num1} multiply by ${Num2}?`;
const correctAns = Num1*Num2;
formEl.addEventListener("submit", ()=>{
const userAns = +inputEl.value;
if (userAns === correctAns){
score++;
updatelocalStorage();
}else{
score--;
updatelocalStorage();
}
});
function updatelocalStorage(){
localStorage.setItem("score", JSON.stringify(score));
}