-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
87 lines (78 loc) · 3.21 KB
/
main.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
function gethistory(){
return document.getElementById("history-val").innerText;
}
function printhistory(num){
document.getElementById("history-val").innerText = num;
}
function getoutput(){
return document.getElementById("output-val").innerText;
}
function commasep(op){
if(op=="-"){//after backspacing -99 2 times only - is op hence set output to null
return "";
}
var n = Number(op);
var val = n.toLocaleString("en"); // to convert like 999,999,999
return val;
}
function printoutput(op){
if(op==""){ // for if empty
document.getElementById("output-val").innerText = "";
}
else{
document.getElementById("output-val").innerText = commasep(op);} // convert to comma seperated as 999,999
}
function nocomma(num){ // get number from the comma seperated string
return Number(num.replace(/,/g,""));
}
var operators = document.getElementsByClassName("operators"); // all operators
for(var i=0; i<operators.length;i++){
operators[i].addEventListener("click",function(){ //an operator is clicked
if(this.id == "clear"){ // to clear all
printhistory("");
printoutput("");
}
else if(this.id=="backspace"){
var output = getoutput();
output = String(nocomma(output));// string output with no commas
output = output.substr(0,output.length - 1);// eliminated last char
printoutput(output);
/*var history = gethistory();
history = history.substr(0,history.length - 1);
printhistory(history);*/
}
else{ // for other operators
var output = getoutput();
var history = gethistory();
if(output=="" && history!=""){ //for changing of operators
if(isNaN(history[history.length -1])){
history = history.substr(0,history.length -1);
}
}
if(output!="" || history!=""){ // if history and output are null then operator must not be entered or if output is null but not history it means change of operator
output = output==""? output : String(nocomma(output)); // depending on the condition outout ="" or convert to no comma string
history = history + output;
if(this.id =="="){ // for evaluation command
var result = eval(history);
printoutput(result);
printhistory("");
}
else{ // for operators other than clear, backspace and =
history = history + this.id; // add operator to history
printhistory(history);// update history
printoutput("") // set output to "" so that new no. can be written
}
}
}
});
}
var numbers = document.getElementsByClassName("numbers");
for(var i=0; i<numbers.length;i++){
numbers[i].addEventListener("click",function(){
var output = nocomma(getoutput());
if (output!=NaN){
output = output + this.id;
printoutput(output);
}
});
}