-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
94 lines (74 loc) · 2.16 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
89
90
91
92
93
94
var reference_point=0;
function getHistory() {
return document.getElementById("history-value").innerText;
}
function printHistroy(num) {
document.getElementById("history-value").innerText = num;
}
function getOutput() {
return document.getElementById("output-value").innerText;
}
function printOutput(num) {
if(num==""){
document.getElementById("output-value").innerText=num;
}else{
document.getElementById("output-value").innerText=getFormattedNumber(num);
}
}
//for formatting text...................................
function getFormattedNumber(num){
var n = Number(num);
var value = n.toLocaleString("en");
return value;
}
function reverseNumberFormat(num){
return Number(num.replace(/,/g,''));
}
//OPERATORS............................................
$('.operator').click(function () {
if(this.id=='clear'){printOutput('');printHistroy(''); reference_point=0;}
else if(this.id=='backspace'){
var output = reverseNumberFormat(getOutput()).toString();
if(reference_point==1){
reference_point=0;
}
else{
output=output.slice(0,output.length-1);
//output = Math.floor(output/10);
}
printOutput(output);
}
else{
var output = getOutput();
var history= getHistory();
if(output!=''){
output = reverseNumberFormat(getOutput());
history=history+output;
if(this.id=='equal'){
var result= eval(history);
printOutput(result);
printHistroy("");
}
else{
history=history+this.innerHTML;
printHistroy(history);
printOutput("");
}
}
}
})
//NUMBERS..............................................
$('.number').click(function (){
var output = reverseNumberFormat(getOutput());
if(output != NaN && this.id != 'point'){
output = output.toString();
output=output+this.innerHTML;
if(reference_point==1){output/=10; reference_point=0;}
printOutput(output);
}
if(this.id=='point'){
reference_point=1;
output=output+'.';
document.getElementById("output-value").innerText=output;
}
})