-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
107 lines (93 loc) · 3.24 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
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
const primDisplay = document.querySelector('#primary-display')
const secondDisplay = document.querySelector('#secondary-display')
const numButtons = document.querySelectorAll('.number')
const operatorButtons = document.querySelectorAll('.operatorBtn')
const equalBtn = document.querySelector('#equalBtn')
const dotBtn = document.querySelector('#dotBtn')
const delBtn = document.querySelector('#deleteBtn')
const clearBtn = document.querySelector('#clearBtn')
let resetScreen = false
let operand = null
let firstNum = secondNum = ''
let roundTo = 4
let maxDigits = 9
numButtons.forEach(btn => btn.addEventListener('click', () => enterNumber(btn.textContent)))
operatorButtons.forEach(btn => btn.addEventListener('click', () => enterOperation(btn.textContent)))
equalBtn.addEventListener('click', evaluate)
dotBtn.addEventListener('click', addDot)
delBtn.addEventListener('click', delChar)
clearBtn.addEventListener('click', clear)
window.addEventListener('keydown', keyboardHandler)
function keyboardHandler(e){
if(e.key >= 0 && e.key < 10) enterNumber(e.key)
if(e.key === 'Enter' || e.key ==='=') evaluate()
if(e.key === '+' || e.key === '-' || e.key === '*' || e.key === '/') enterOperation(e.key)
if(e.key === '.') addDot()
if(e.key === 'Delete' || e.key === 'Backspace') delChar()
console.log(e.key)
}
// https://www.peterlunch.com/snippets/javascript-round
function roundToX(num, decimals) {
return +(Math.round(num + "e" + decimals) + "e-" + decimals);
}
function clear() {
primDisplay.textContent = '0'
secondDisplay.textContent = ''
firstNum = secondNum = ''
operand = null
resetScreen = false
}
function delChar() {
primDisplay.textContent.length <= 1
? primDisplay.textContent = '0'
: primDisplay.textContent = primDisplay.textContent.slice(0, -1)
}
function addDot() {
if(resetScreen) clearPrimDisplay()
if(primDisplay.textContent.includes('.')) return
if(primDisplay.textContent === '') primDisplay.textContent = '0'
primDisplay.textContent += '.'
}
function enterOperation(operator) {
if(operand !== null) evaluate()
firstNum = primDisplay.textContent
operand = operator
secondDisplay.textContent = firstNum + " " + operand
resetScreen = true
}
function evaluate() {
if(operand === null || resetScreen) return
secondNum = primDisplay.textContent
primDisplay.textContent = roundToX(operate(operand, firstNum, secondNum), roundTo)
secondDisplay.textContent = `${firstNum} ${operand} ${secondNum} =`
operand = null
resetScreen = true
}
function enterNumber(num) {
if(primDisplay.textContent === '0' || resetScreen) clearPrimDisplay()
if(primDisplay.textContent.length > maxDigits) resetScreen = true
primDisplay.textContent += num
}
function clearPrimDisplay(){
primDisplay.textContent = ''
resetScreen = false
}
add = (num1,num2) => num1 + num2
subtract = (num1,num2) => num1 - num2
multiply = (num1,num2) => num1 * num2
divide = (num1,num2) => num1 / num2
function operate(operator, num1, num2) {
num1 = Number(num1)
num2 = Number(num2)
switch(operator) {
case '+':
return add(num1, num2)
case '-':
return subtract(num1, num2)
case '/':
if(num2 === 0) return "Division by 0 is not allowed."
return divide(num1, num2)
case '*':
return multiply(num1, num2)
}
}