-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCalculatorModel.js
More file actions
86 lines (77 loc) · 2.57 KB
/
CalculatorModel.js
File metadata and controls
86 lines (77 loc) · 2.57 KB
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
const DEFAULT_START_VALUE = 0;
class CalculatorModel
{
static Numbers = [];
static Operators = [];
static MostRecentNumber = DEFAULT_START_VALUE;
static MostRecentOperator = "";
static MostRecentResult = DEFAULT_START_VALUE;
static CalculateResult()
{
return this.Calculate(this.Numbers, this.Operators);
}
static Calculate(numbers, operators)
{
var tempNumbers = numbers.slice();
var currentValue = tempNumbers[0];
var currentOperator = "";
tempNumbers.shift();
for(var i = 0; i < operators.length && tempNumbers.length > 0; i++)
{
currentOperator = operators[i];
switch(ElementEvaluator.EvaluateValue(currentOperator))
{
case ElementEnum.PLUS:
currentValue += tempNumbers[0];
break;
case ElementEnum.MINUS:
currentValue -= tempNumbers[0];
break;
case ElementEnum.MULTIPLICATION:
currentValue *= tempNumbers[0];
break;
case ElementEnum.DIVISION:
currentValue /= tempNumbers[0];
break;
default:
currentValue = DEFAULT_START_VALUE;
break;
}
tempNumbers.shift();
}
return currentValue;
}
static AddOperatorToCalculation(tempOperator)
{
this.Operators.push(tempOperator);
this.MostRecentOperator = tempOperator;
}
static AddNumberToCalculation(number)
{
this.Numbers.push(number);
this.MostRecentNumber = number;
}
static GetResult()
{
var result = 0;
if (this.Numbers.length > this.Operators.length && this.Numbers.length > 1 && this.Operators.length > 0)
result = this.CalculateResult();
else
result = DEFAULT_START_VALUE;
this.MostRecentResult = result;
return result;
}
static Clear()
{
this.Numbers = [];
this.Operators = [];
}
static GetAmountOfNumbers()
{
return this.Numbers.length;
}
static GetAmountOfOperators()
{
return this.Operators.length;
}
}