-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathNumberBuilder.js
72 lines (63 loc) · 2.78 KB
/
NumberBuilder.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
const DEFAULT_FLOAT_VALUE = 0;
const DEFAULT_STRING_VALUE = "";
class NumberBuilder
{
static NumberAsString = DEFAULT_STRING_VALUE;
static NewNumber()
{
this.NumberAsString = DEFAULT_STRING_VALUE;
}
static GetNumberFromString()
{
var conversionPossible = ElementEvaluator.ValueIsNumber(this.NumberAsString);
if (conversionPossible) return parseFloat(this.NumberAsString);
var numberWithoutDecimal = this.NumberAsString.slice(0,this.NumberAsString.indexOf(ElementEvaluator.EvaluateElement(ElementEnum.DECIMAL)));
conversionPossible = ElementEvaluator.ValueIsNumber(numberWithoutDecimal);
if (conversionPossible) return parseFloat(numberWithoutDecimal);
return DEFAULT_FLOAT_VALUE;
}
static Append(addedCharacters)
{
if (ElementEvaluator.ValueIsOperator(addedCharacters)) return;
if (ElementEvaluator.EvaluateValue(addedCharacters) == ElementEnum.DECIMAL && this.NumberAsString.includes(ElementEvaluator.EvaluateElement(ElementEnum.DECIMAL)))
return;
var multiplication = 1;
var tempAddedCharacters = addedCharacters;
for(var i = 0; i < tempAddedCharacters.length; i++)
{
if(tempAddedCharacters.includes(ElementEvaluator.EvaluateElement(ElementEnum.SIGN_CHANGE)))
{
multiplication *= -1;
var foundIndex = tempAddedCharacters.indexOf(ElementEvaluator.EvaluateElement(ElementEnum.SIGN_CHANGE));
var foundLength = ElementEvaluator.EvaluateElement(ElementEnum.SIGN_CHANGE).length;
tempAddedCharacters = tempAddedCharacters.slice(0,foundIndex);
}
}
this.NumberAsString += tempAddedCharacters;
var temp = this.NumberAsString;
this.NumberAsString = (multiplication * this.GetNumberFromString()).toString();
if (temp !== this.NumberAsString)
{
if (this.NumberAsString.includes("-"))
{
var signChanged = "-" + temp;
if (signChanged !== this.NumberAsString)
this.NumberAsString += ".";
}
else if (temp.includes("-"))
{
var tempNumberAsString = "-" + this.NumberAsString;
if (temp !== tempNumberAsString)
this.NumberAsString += ".";
}
else
{
this.NumberAsString += ".";
}
}
}
static BuildNumber()
{
return this.GetNumberFromString();
}
}