-
Notifications
You must be signed in to change notification settings - Fork 0
/
calculator.js
44 lines (37 loc) · 1.33 KB
/
calculator.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
window.addEventListener('load', addOnClick);
/*
* The function that's called when the window is loaded,
* and takes care of adding all the event listeners
*/
function addOnClick() {
var inputs = document.getElementsByTagName('input');
for (var i = 0; i < inputs.length; i++) {
// console.log(inputs[i]);
inputs[i].addEventListener('click', userClick);
}
};
/*
*This function catches the event when user clicks on a button.
*If the clicked button is different from equals and clear,
*then it will update the display adding the new event value.
*If the clicked button is equals,
*then it will evaluate the math expression of the display and update it with the result.
*If the clicked button is clear,
*then it will empty the display.
*/
function userClick(event) {
var displayValue = document.getElementById('display').value;
var eventValue = event.target.attributes.value.nodeValue;
console.log(displayValue);
console.log(eventValue);
if ((eventValue != '=') && (eventValue != 'c')) {
displayValue = displayValue + eventValue;
document.getElementById('display').value = displayValue;
} else if (eventValue === '=') {
var result = eval(displayValue);
console.log(result);
document.getElementById('display').value = result;
} else if (eventValue === 'c') {
document.getElementById('display').value = '';
}
};