forked from Moslihbadr/multiplication-game
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
61 lines (51 loc) · 1.54 KB
/
main.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
// variables
const happy = '😀' ;
const sad = '😔' ;
let emoji = document.getElementById("emoji") ;
let score = document.getElementById("score") ;
let num1 = document.getElementById("num1") ;
let num2 = document.getElementById("num2") ;
let input = document.getElementById("input") ;
let btn = document.getElementById("btn") ;
let soundTrack = document.getElementById("soundTrack");
// play the soundTrack
function autoPlay() {
soundTrack.load;
soundTrack.play();
}
// change the num1 and num2 randomly(between 1 and 10)
function setNum() {
let n = Math.floor(Math.random()*10) + 1;
let m = Math.floor(Math.random()*10) + 1;
num1.innerText = n ;
num2.innerText = m ;
}
// clear the input after submit the answer
function clearInput() {
input.value = '';
}
// compare the result and the given answer
function checkAnswer(){
const result = parseInt(num1.innerText) * parseInt(num2.innerText) ;
const value = parseInt(input.value);
if ( value === result){
emoji.innerHTML = happy;
score.innerText = parseInt(score.innerText) + 1;
}else{
emoji.innerHTML = sad;
const audio = new Audio('./mix_02s (audio-joiner.com).mp3');
audio.play();
if (parseInt(score.innerText)>0){
score.innerText = parseInt(score.innerText) - 1;
};
};
};
// main functions
setNum();
btn.addEventListener('click',()=> {
checkAnswer();
setNum();
clearInput();
autoPlay();
input.focus();
});