-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
99 lines (89 loc) · 2.58 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
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
// set var dom
const currentAnswer = document.getElementById("currentAnswer");
const gameResult = document.getElementById("gameResult");
const currentQuestion = document.getElementById("currentQuestion");
const green = document.getElementById("green");
const red = document.getElementById("red");
//set element audio
const restartSound = document.getElementById("restartSound");
const submitSound = document.getElementById("submitSound");
const successSound = new Audio(
"https://www.pacdv.com/sounds/applause-sounds/app-29.wav"
);
const failureSound = new Audio(
"https://www.pacdv.com/sounds/voices/no-thats-not-gonna-do-it.wav"
);
const noAnswerSound = new Audio(
"https://www.pacdv.com/sounds/mechanical_sound_effects/glass_breaking_2.wav"
);
let resultComp = 0;
let correctCount = 0;
let failCount = 0;
const correctLimit = 3;
const failLimit = 3;
// show firts compute
computeMultiplication();
displayMessage(green, correctCount);
displayMessage(red, failCount);
/**
* compute multilipcation and display
*/
function computeMultiplication() {
var factorOne = Math.floor(Math.random() * 10);
var factorTwo = Math.floor(Math.random() * 10);
resultComp = factorOne * factorTwo;
displayMessage(currentQuestion, `${factorOne} X ${factorTwo}`);
}
/**
* display message in div
* @param {*} text
*/
function displayMessage(element, text) {
element.innerText = text;
}
// click submit
function handleSubmit() {
// check limit in correctCount
if (correctCount >= correctLimit) {
successSound.play();
displayMessage(
gameResult,
"Great job! Play again to strengthen your knowledge"
);
return false;
}
// check limit in failCount
if (failCount === failLimit) {
failureSound.play();
displayMessage(gameResult, "You can do better, try again");
return false;
}
// check if empty text box
if (currentAnswer.value === "" || currentAnswer.value === "undifined") {
displayMessage(gameResult, "Please enter your answer and press Submit");
noAnswerSound.play();
return false;
} else {
displayMessage(gameResult, "");
}
if (+currentAnswer.value != resultComp) {
failCount++;
displayMessage(red, failCount);
} else {
correctCount++;
displayMessage(green, correctCount);
}
computeMultiplication();
currentAnswer.value = "";
submitSound.play();
}
// click restart
function handleRestart() {
restartSound.play();
computeMultiplication();
resultComp = correctCount = failCount = 0;
displayMessage(green, correctCount);
displayMessage(red, failCount);
displayMessage(gameResult, "");
currentAnswer.value = "";
}