-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
130 lines (115 loc) · 3.87 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
class Calculator{
constructor(prevopText,curropText){
this.prevopTextElement=prevopText;
this.curropTextElement=curropText;
this.reset();
}
delete(){
this.curropText=this.curropText.toString().slice(0,-1);
}
reset(){
this.prevopText='';
this.curropText='';
this.operation=undefined;
}
compute(){
let prev=parseFloat(this.prevopText);
let curr=parseFloat(this.curropText);
console.log("printing");
console.log(prev,curr);
if(isNaN(prev)||isNaN(curr))return;
switch(this.operation){
case '+': prev=prev+curr;
break;
case '-': prev=prev-curr;
break;
case '*': prev=prev*curr;
break;
case '÷': prev=prev/curr;
break;
default:
return;
}
this.prevopText='';
this.curropText=prev.toString();
this.operation=undefined;
}
chooseOperation(number){
if(this.curropText==='')return;
if(this.prevopText!=='' && this.prevopText!='0÷'){
this.compute();
}
this.operation=number;
this.prevopText=this.curropText;
this.curropText='';
}
appendNumber(number){
if(number==='.' && this.curropText.includes('.'))return ;
// console.log("inside appendNumber");
// console.log(this.curropText.toString());
// console.log(number.toString());
if(number=='0' && this.operation=='÷' && this.prevopText=='0'){window.alert("0/0 is not a number.Press reset to proceed");this.reset();return;}
this.curropText=this.curropText.toString()+number.toString();
// console.log( this.curropText);
}
formatDisplay(number){
// let floatnum= parseFloat(number);//decimal point can't be parsed TODO
// if(isNaN(floatnum))return '';
// return floatnum.toLocaleString('en');
let buf=number.toString();
let integerpart=parseFloat(buf.split('.')[0]);
let decimalpart=buf.split('.')[1];
let integerDisplay;
if(isNaN(integerpart)){
integerDisplay='';
}else{
integerDisplay=integerpart.toLocaleString('en',{maximumFractionDigits:0})
}
if(decimalpart!=null){
return `${integerDisplay}.${decimalpart}`;
}else{
return integerDisplay;
}
}
updateDisplay(){
this.curropTextElement.innerText=this.formatDisplay(this.curropText);
if(this.operation!=null&&this.operation!=undefined){
this.prevopTextElement.innerText=`${this.formatDisplay(this.prevopText)}${this.operation}`;
}else{
this.prevopTextElement.innerText=this.prevopText;
}
}
}
const num=document.querySelectorAll("[data-num]");
const del=document.querySelector("[data-del]");
const operation=document.querySelectorAll("[data-operation]");
const reset=document.querySelector("[data-reset]");
const equals=document.querySelector("[data-equals]");
const prevopTextElement=document.querySelector("[data-prev-op]");
const curropTextElement=document.querySelector("[data-curr-op]");
const calculator=new Calculator(prevopTextElement,curropTextElement);
equals.addEventListener('click',()=>{
calculator.compute();
calculator.updateDisplay();
})
reset.addEventListener('click',()=>{
calculator.reset();
calculator.updateDisplay();
})
del.addEventListener('click',()=>{
calculator.delete();
calculator.updateDisplay();
})
num.forEach((btn)=>{
btn.addEventListener('click',()=>{
console.log(btn.innerText);
calculator.appendNumber(btn.innerText);
calculator.updateDisplay();
});
});
operation.forEach((btn)=>{
btn.addEventListener('click',()=>{
calculator.chooseOperation(btn.innerText);
calculator.updateDisplay();
})
})