-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwhyMe.js
More file actions
144 lines (122 loc) · 3.98 KB
/
whyMe.js
File metadata and controls
144 lines (122 loc) · 3.98 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// ******** WHY ME? SECTION OF WEBSITE ********
// ******** Variables ********
// Initialise calculator modal elements as variables in JS
const cModal = document.getElementById("cModal");
const openCalc = document.getElementById("openCalc");
const closeCalc = document.getElementById("closeCalc");
// Initialise calculator elements as variables in JS
let calculation = document.getElementById("calculation__value");
let output = document.getElementById("output__value");
// Declare variables to hold values/data when calculating input to calculation and output elements
let calcData;
let outputData
// Initialise variable to indicate whether equals was last button pressed
let sumDone = false;
// Initialise an array of number buttons
const numbers = document.getElementsByClassName("number");
//Initialise an array of operator buttons
const operators = document.getElementsByClassName("operator");
// ******** Functions ********
// Function to transform num into comma seperated number, for displaying (in setOutput())
function formatNum(num) {
const f = Number(num);
return f.toLocaleString("en");
}
// Function to transform comma seperated number in to non-comma seperated, for clearing last entry (operators())
function unformatNum(num) {
return Number(num.replace(/,/g,""));
}
// Functions to get/set 'calculation' string value
function getCalculation() {
return calculation.innerText;
}
function setCalculation(val) {
calculation.innerText = val;
}
// Functions to get/set calculator 'output' number value
function getOutput() {
return output.innerText;
}
function setOutput(val) {
output.innerText = formatNum(val);
}
// Function to update output data before setting output field
function updateOutputData() {
outputData = unformatNum(getOutput());
// Lines to remove initial emoji etc
if(isNaN(outputData)) {
setOutput(this.id);
setCalculation("");
}
//Clear rather than append if last button was =
else if (sumDone == true) {
outputData = this.id;
setOutput(outputData);
sumDone = false;
}
//Append
else {
outputData = outputData + this.id;
setOutput(outputData);
}
}
// Function to identify and action operators
function actionOperators() {
if (this.id == "clear") {
setCalculation("");
setOutput("");
}
else if (this.id == "CE") {
outputData = unformatNum(getOutput()).toString(); //remove commas and convert back to string
outputData = outputData.substring(0, outputData.length-1);
if(isNaN(outputData)) { // lines to remove initial emoji value etc
outputData = 0;
setCalculation("");
}
setOutput(outputData);
}
else {
outputData = unformatNum(getOutput())
if(isNaN(outputData)) { // lines to remove initial emoji value etc
setCalculation("0"+this.id);
output.innerText = "";
}
else {
if (this.id == "=") {
calcData = getCalculation() + outputData;
let result = eval(calcData);
setOutput(result);
setCalculation("");
sumDone = true;
}
else {
calcData = getCalculation() + outputData + this.id;
setCalculation(calcData);
output.innerText = "";
}
}
}
}
// ******** Event Listeners ********
// Create listeners to append a number to output value when a number button is pressed
for(let i=0; i<numbers.length; i++) {
numbers[i].addEventListener('click', updateOutputData)
};
// When the user clicks 'here' text, open the calculator modal
openCalc.addEventListener('click', () => {
cModal.style.display = "block";
});
// When the user clicks on close (x), close the calculator modal
closeCalc.addEventListener('click', () => {
cModal.style.display = "none";
});
// When the user clicks anywhere outside of the calculator modal, close it
window.addEventListener('click', (e) => {
if (e.target == cModal) {
cModal.style.display = "none";
}
});
// Create listeners to respond to operators being pressed
for(let i=0; i<operators.length; i++) {
operators[i].addEventListener('click', actionOperators)
};