-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
104 lines (97 loc) · 3.73 KB
/
app.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
function getHeader(){
return document.getElementById("header").innerText;
}
function getTotal(){
return document.getElementById("total").innerText;
}
function evaluateit(){
var no1=getHeader();
var no2=getTotal();
var a=no1.charAt(no1.length);
var no11=reverseNumber(no1.substring(0,no1.length-1));
var no22=reverseNumber(no2);
if(a== '/'){return no11/no22;}
if(a=='x'){return no11*no22;}
if(a=='-'){return no11-no22;}
if(a=='+'){return no11+no22;}
}
function updateHeader(num){
document.getElementById("header").innerText=num;
}
function updateTotal(total){
if (total==""){
document.getElementById("total").innerText=total;
}
else{
document.getElementById("total").innerText=getFormattedNumber(total);
}
}
function getFormattedNumber(num){
var n=Number(num);
var value= n.toLocaleString("en");
return value;
}
function reverseNumber(num){
return Number(num.replace(/,/g,''));
}
var operator = document.getElementsByClassName("op");
for(var i=0;i<operator.length;i++){
operator[i].addEventListener('click',function(){
if(this.id=='allcl'){
document.getElementById("total").textContent="";
document.getElementById("header").textContent="";
}
if(this.id=='cl'){
if(document.getElementById('total').textContent.length!='')
{//
var str=getTotal();
var num=reverseNumber(str);
var str = num.toString(10);
var str = getFormattedNumber(str);
document.getElementById('total').textContent=str.substring(0,str.length-1);
}
else{
if(getHeader().charAt(getHeader().length-1)=='/'||getHeader().charAt(getHeader().length-1)=='x'||getHeader().charAt(getHeader().length-1)=='-'||getHeader().charAt(getHeader().length-1)=='+'){
var str=getHeader();
document.getElementById('total').textContent=str.substring(0,str.length-1);
document.getElementById('header').textContent='';
}
else{
var str=getHeader();
document.getElementById('header').textContent=str.substring(0,str.length-1);
}
}
}
if(this.id=='div'||this.id=='mul'||this.id=='sub'||this.id=='add'){
document.getElementById('header').textContent=document.getElementById('total').textContent+document.getElementById(this.id).textContent;
document.getElementById('total').textContent="";
}
if(this.id=='answer'){
//document.getElementById('total').textContent=evaluateit();
//document.getElementById('header').textContent=document.getElementById('header').textContent+document.getElementById('total').textContent;
var str=getHeader();
var str2=getTotal();
document.getElementById('header').textContent= str+str2;
//var numu=evaluateit();
var a=str.charAt(str.length-1);
var no1=reverseNumber(str.substring(0,str.length-1));
var no2=reverseNumber(str2);
var no0=0;
if(a== '/'){var no0=(no1/no2);}
if(a=='x'){var no0= (no1*no2);}
if(a=='-'){var no0=(no1-no2);}
if(a=='+'){var no0=(no1+no2);}
document.getElementById('total').textContent=no0.toString(10);
}
})
}
var number= document.getElementsByClassName("no");
for(var i=0;i<number.length;i++){
number[i].addEventListener('click',function(){
var output = reverseNumber(getTotal());
if(output!=NaN){
output=output+this.id;
updateTotal(output);
}
})
}