-
Notifications
You must be signed in to change notification settings - Fork 642
/
Copy pathscript.js
88 lines (84 loc) · 1.91 KB
/
script.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
angular.module('app', []).controller('GameCtrl', ['$scope', '$timeout', function($scope, $timeout){
$scope.variables = {};
$scope.correct = 0;
$scope.wrong = 0;
$scope.timer = 0;
_start = false;
_end = false;
var _answer = null;
var _answerCorrect = null;
var _sym = ['-','+','÷','x'];
function _initialize() {
$scope.variables = {
'x': Math.floor(Math.random() * (10)) + 1,
'y': Math.floor(Math.random() * (10)) + 1,
};
$scope.symbol = _sym[Math.round(Math.random()*3)];
switch ($scope.symbol) {
case '-':
_answer = $scope.variables.x - $scope.variables.y;
break;
case '+':
_answer = $scope.variables.x + $scope.variables.y;
break;
case 'x':
_answer = $scope.variables.x * $scope.variables.y;
break;
default:
var x = $scope.variables.x;
$scope.variables.x = x * $scope.variables.y;
_answer = x;
break
}
_answerCorrect = null;
$scope.answer = null;
}
angular.extend($scope, {
isAnswer: function (){
return _answerCorrect === true;
},
isIncorrect: function (){
return _answerCorrect === false;
},
accuracy: function(){
return $scope.correct / ($scope.correct + $scope.wrong) * 100;
},
noGame: function(){
return !_start && !_end;
},
startGame: function(){
_start = true;
_end = false;
$scope.correct = 0;
$scope.wrong = 0;
$scope.timer = 0;
$timeout($scope.increaseTimer, 1000);
},
playingGame: function(){
return _start && !_end;
},
endGame: function() {
return _start && _end;
},
increaseTimer: function(){
$scope.timer++;
if($scope.timer == 60){
_end = true;
} else {
$timeout($scope.increaseTimer, 1000);
}
},
checkAnswer: function(){
_answerCorrect = parseInt($scope.answer) == _answer;
if(_answerCorrect) {
$scope.correct++;
_initialize()
}else{
$scope.wrong++;
$scope.answer = null;
}
}
});
_initialize();
}
]);