-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
133 lines (109 loc) · 3.05 KB
/
script.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
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
//Calculator Variables
let firstNum;
let secondNum;
let currentOperator = null;
let displayValue = 0;
//Elements
const numberDisplay = document.querySelector('.number-display');
const operationsKeys = document.querySelectorAll('.operations button');
const numKeys = document.querySelectorAll('.numpad button');
//Arithmetic Functions
function add (num1, num2) {
return num1 + num2;
};
function subtract (num1, num2) {
return num1 - num2;
};
function multiply (num1, num2) {
return num1 * num2;
};
function divide (num1, num2) {
if (num2 == 0) {
return null;
}
return num1 / num2;
};
function operate(operator, num1, num2) {
num1 = +num1;
num2 = +num2;
let result;
if (typeof num1 !== 'number' || typeof num2 !== 'number') {
throw new TypeError('Inputs need to be numbers');
}
switch (operator) {
case '+':
result = add(num1, num2);
break;
case '-':
result = subtract(num1, num2);
break;
case '*':
result = multiply(num1, num2);
break;
case '/':
result = divide(num1, num2);
break;
}
console.log(`${num1} ${operator} ${num2}`);
displayValue = result
updateDisplay(displayValue);
return result;
};
function clearDisplay() {
firstNum = '';
secondNum = '';
displayValue = '';
currentOperator = null;
updateDisplay (displayValue);
}
//takes an operator and 2 numbers and then calls one of the above functions on the numbers.
function updateDisplay (currentValue) {
numberDisplay.textContent = currentValue;
return;
};
function handleNumpadInput (e) {
const numInput = e.target.name;
if(displayValue === 0) {
displayValue = numInput;
} else {
displayValue = parseInt(displayValue + numInput.toString());
}
updateDisplay(displayValue);
}
function setOperation (operationPressed) {
if (currentOperator !== null) {
saveCurrentNumber(displayValue);
firstNum = operate(currentOperator, firstNum, secondNum);
}
currentOperator = operationPressed;
saveCurrentNumber(displayValue);
displayValue = '';
}
function saveCurrentNumber (num) {
(!firstNum) ? firstNum = num : secondNum = num;
return num;
}
//Add Listeners to keys
numKeys.forEach(function(button) {
button.addEventListener("click", handleNumpadInput);
});
operationsKeys.forEach(function(button) {
button.addEventListener("click", e => {
const operationPressed = e.target.name;
switch(operationPressed) {
case '=':
if (!currentOperator) {
return;
}
saveCurrentNumber(displayValue);
firstNum = operate(currentOperator, firstNum, secondNum);
currentOperator = null;
break;
case 'clear':
clearDisplay();
break;
default:
setOperation(operationPressed);
}
});
});