-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
151 lines (122 loc) · 3.49 KB
/
app.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
var $display;
var roundsPlayed= [];
var time = 3000;
var three_seconds = 3
var initialize = function(){
$display = $('#display');
bindEvents();
}
var randomNumber = function(min,max) {
return parseInt(Math.floor(Math.random()*(max-min+1)+min));
}
var randomOperator = function() {
var operators = ["+", "-", "/", "*"];
return operators[Math.floor(Math.random() * operators.length)]
}
var generateEquation = function() {
var min = 20,
max = 200,
firstRandomNumber = randomNumber(min, max),
secondRandomNumber = randomNumber(min, max),
operator = randomOperator(),
answer;
switch (operator) {
case "+":
answer = firstRandomNumber + secondRandomNumber;
break;
case "-":
answer = firstRandomNumber - secondRandomNumber;
break;
case "/":
answer = firstRandomNumber / secondRandomNumber;
break;
case "*":
answer = firstRandomNumber * secondRandomNumber;
break;
}
return {
firstRandomNumber: firstRandomNumber,
operator: operator,
secondRandomNumber: secondRandomNumber,
answer: answer
}
}
function shuffle(array) {
var currentIndex = array.length, temporaryValue, randomIndex ;
// While there remain elements to shuffle...
while (0 !== currentIndex) {
// Pick a remaining element...
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;
// And swap it with the current element.
temporaryValue = array[currentIndex];
array[currentIndex] = array[randomIndex];
array[randomIndex] = temporaryValue;
}
return array;
}
cancel = true;
function startTimer(duration, display) {
var timer = duration, minutes, seconds;
var interval = setInterval(function () {
seconds = parseInt(timer % 60, 10);
seconds = seconds < 10 ? seconds : seconds;
$display.text(seconds);
console.log(timer)
if (timer == -1) {
$display.text("");
clearInterval(interval)
easyGame();
} else if(--timer < -1) {
$display.text("");
}
},1000);
}
var easyGame = function(){
$('#easy').addClass('animated rubberBand');
setTimeout(function() {
$('#easy').removeClass('animated rubberBand');
}, 2000)
var operators = ["+", "-", "/", "*"];
for(var i = 1; i<5; i++){
$('#operator'+i).html(newShuffle = shuffle(operators).splice(0, 1))
$('#operator'+i).attr("data-id", newShuffle.splice(0, 1))
}
var answer = generateEquation();
while (answer.answer % 1 !== 0 || answer.answer < 50 || answer.answer > 999) {
answer = generateEquation();
}
$display.text(answer.firstRandomNumber + " " + "?" + " "+ answer.secondRandomNumber + " = " + answer.answer)
$(".operator").on("click", function(){
if(this.getAttribute("data-id") === answer.operator){
$('#progress').html('Correct! Верно! Кichtig! 正確! Correctto!<br><button id="continue">CONTINUE</button>');
$("#continue").on("click", function(){
$('#progress').html("");
$display.html("");
roundsPlayed++;
time -= 100;
startTimer(three_seconds, display);
})
} else {
$('#progress').html('WRONG! come one, you can do better than that! Неправильно! Falsch! 錯! Sbagliato!<br><button id="startAgain">START AGAIN</button>'); //alert("wrong answer")
$("#startAgain").on("click", function(){
$display.html("");
time = 2500
startTimer(three_seconds, display);
})
}
})
setTimeout(function() {
$('.operator').html('?')
}, time);
}
var bindEvents = function() {
var three_seconds = 3,
display = $("#display");
$('#easy').on('click', function(){
startTimer(three_seconds, display)
});
}
$(function() {
initialize();
});