-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.js
More file actions
115 lines (101 loc) · 3.29 KB
/
main.js
File metadata and controls
115 lines (101 loc) · 3.29 KB
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
/*
_______________________________________
Author: NemoNet aka The Young Programmer 🏅
Technologies: HTML, CSS & Javascript
Code Editor: NemoNet Editor
Date: Monday 3 May, 2021
All thanks to @Chibuzor
_______________________________________
*/
// read history value from UI
function getHistoryValue(){
return document.getElementById("history").innerText;
}
// write stuff to history area on UI
function printHistoryValue(num){
document.getElementById("history").innerText = num;
}
// read output value from UI
function getOutputValue(){
return document.getElementById("output").innerText;
}
// write stuff to output area on UI
function printOutputValue(num){
if (num === ""){
document.getElementById("output").innerText = num;
}
else{
document.getElementById("output").innerText = getFormattedNumber(num);
}
}
// nicely formats output string value to a comma separated value
function getFormattedNumber(num){
if (num == "-"){
return "";
}
var n = Number(num);
var value = n.toLocaleString("en");
return value;
}
// remove comma separation format from formatted output
function reverseNumberFormat(num){
return Number(num.replace(/,/g, ""));
}
addEventListener("DOMContentLoaded", function() {
// listen for operator keys click events
var operators = document.getElementsByClassName("op__key");
var len = operators.length;
for (i=0; i<len; i++){
operators[i].addEventListener("click", function(){
if (this.id == "clear"){
printHistoryValue("");
printOutputValue("");
}
else if (this.id == "backspace"){
var output = reverseNumberFormat(getOutputValue()).toString();
// check whether output has a value then remove last character and print to UI
if (output){
output = output.substr(0, output.length - 1);
printOutputValue(output);
}
}
else{
var output = getOutputValue();
var history = getHistoryValue();
// truncate non-numeric type last character from history value
if (output === "" && history !== ""){
if (isNaN(history[history.length - 1])){
history = history.substr(0, history.length - 1);
}
}
if (output !== "" || history !== ""){
// tenary operation to set output to empty when it is empty
output = output === ""? output : reverseNumberFormat(output);
history += output;
if (this.id === "="){
var result = eval(history);
printOutputValue(result);
printHistoryValue("");
}
else{
history += this.id;
printHistoryValue(history);
printOutputValue("");
}
}
}
})
}
// listen for number keys click events
var numbers = document.getElementsByClassName("num__key");
var val = numbers.length;
for (i=0; i<val; i++){
numbers[i].addEventListener("click", function(){
var output = reverseNumberFormat(getOutputValue());
if (!isNaN(output)){
output += this.id;
printOutputValue(output);
}
})
}
});