-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
64 lines (55 loc) · 1.91 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
const display = document.querySelector('#display');
const numbers = document.querySelectorAll('.number');
const operators = document.querySelectorAll('.operator');
const clearButton = document.querySelector('#clear');
const backSpace = document.querySelector('#backspace');
const equalsTo = document.querySelector('#equals');
const percentageButton = document.querySelector('.percentage');
// Display in input *code*
numbers.forEach((number) => {
number.addEventListener('click', (e) => {
let value = Number(number.innerText);
displayValue(value);
})
})
operators.forEach((operator) => {
operator.addEventListener('click', () => displayControls(operator));
})
function displayControls(operator) {
if (display.value === '' && (operator.innerText !== '+' && operator.innerText !== '-')) {
//do nothing
console.log(operator.innerText);
} else if (display.value === '+' || display.value === '-') {
// do nothing
} else if (['+','-','/','*'].includes(display.value.at(display.value.length - 1))) {
// do nothing
} else if (operator.innerText === '%' && display.value.at(display.value.length - 1) === '%'){
//do nothing
} else {
let value = operator.innerText;
displayValue(value);
}
}
function displayValue(value) {
display.value += value;
}
// clear button
clearButton.addEventListener('click', (e) => {
display.value = '';
})
//backspace button
backSpace.addEventListener('click', (e) => {
display.value = display.value.slice(0,display.value.length - 1);
})
//clicking "equalsTo"
equalsTo.addEventListener("click", (e) => {
console.log(display.value);
})
window.addEventListener('keydown', (e) => {
if([1,2,3,4,5,6,7,8,9,0].includes(Number(e.key))) {
displayValue(e.key);
} //else if(['+','-','/','*','%'].includes(e.key)) {
// displayControls(e.key);
// console.log(e);
// };
})