-
Notifications
You must be signed in to change notification settings - Fork 0
/
Calculator.js
90 lines (75 loc) · 2.44 KB
/
Calculator.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
/* Name = Maurya Shubham Roll No = 33 Calculator.css */
function getPreviousValue(){
return document.getElementById('previous-value').innerText;
}
function PrintPreviousValue(num){
document.getElementById('previous-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 = getFormatedNumber(num) ;
}
}
function getFormatedNumber(num){
var n = Number(num);
var value = n.toLocaleString("en");
return value
}
function reverseNumberFormat(num) {
return Number(num.replace(/,/g,''));
}
var sign = document.getElementsByClassName("sign");
for (var i = 0 ; i< sign.length ; i++){
sign[i].addEventListener("click",function(){
if (this.id == "clear"){
PrintPreviousValue("");
PrintOutput("");
}
else if(this.id == "backspace"){
var output = reverseNumberFormat(getOutput()).toString();
if(output){
output = output.substr(0 , output.length-1);
PrintOutput(output);
}
}
else {
var output = getOutput();
var previous = getPreviousValue();
if (output == "" && previous != ""){
if(isNaN(previous[previous.length-1]))
previous = previous.substr(0,previous.length-1);
}
if(output != "" || previous !=""){
output= output==""?
output:reverseNumberFormat(output);
previous = previous + output;
if (this.id == "="){
var output = eval(previous);
PrintOutput(output);
PrintPreviousValue("");
}
else {
previous = previous + this.id
PrintPreviousValue(previous)
PrintOutput("")
}
}
}
});
}
var digit = document.getElementsByClassName("digit");
for (var i = 0 ; i< digit.length ; i++){
digit[i].addEventListener("click",function(){
var output = reverseNumberFormat(getOutput());
if (output != NaN){
output = output + this.id;
PrintOutput(output);
}
})
}