-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
45 lines (39 loc) · 1.27 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
(function(){
let screen = document.querySelector('.screen');
let buttons = document.querySelectorAll('.btn');
let clear = document.querySelector('.btn-clear');
let equal = document.querySelector('.btn-equal');
let special = document.querySelectorAll('.btn-special');
//retrieve data from numbers and "+" "-" that are clicked
buttons.forEach(function(button){
button.addEventListener('click', function(e){
let value = e.target.dataset.num;
screen.value += value;
})
});
//retrieve data from "*" "/" that are clicked
special.forEach(function(button){
button.addEventListener('click', function(e){
if(screen.value !== ''){
let value = e.target.dataset.num;
screen.value += value;
} else {
screen.placeholder = 'Please Enter a Value';
}
})
});
equal.addEventListener('click', function(e){
if(screen.value === ''){
screen.placeholder = 'Please Enter a Value';
} else {
let answer = eval(screen.value);
screen.value = answer;
}
})
clear.addEventListener('click', function(e){
if(screen.value !== ''){
screen.value = '';
screen.placeholder = '';
}
})
})();