-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
120 lines (104 loc) · 3.34 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
// set const values
const calculator = {
displayValue: '0',// you can set default value, count start at zero
firstOperand: null,// you start with no operand like *,+ etc
waitingForSecondOperand: false,// wait untill u input
operator: null,
};
// start putting our values by setting defaults values for calc
function inputDigit(digit) {// initialize our function and pass digit as the parameter
//distructuring
const { displayValue, waitingForSecondOperand } = calculator;// pass two paramenters , initial count and what whe inputs
if (waitingForSecondOperand === true) {// if at initial count
calculator.displayValue = digit;// display zero
calculator.waitingForSecondOperand = false;// waitingForSecondOperand becomes false
} else {
calculator.displayValue = // if at initial state
displayValue === '0' ? digit : displayValue + digit;// increment waitingForSecondOperand (? means tynary)
}
}
// The inputDecimal ()
function inputDecimal(dot) {
if (calculator.waitingForSecondOperand === true) {// that is if we start adding values
calculator.displayValue = '0.';
calculator.waitingForSecondOperand = false;
return;
}
if (!calculator.displayValue.includes(dot)) {
calculator.displayValue += dot;
}
}
// HandleOperators
function handleOperator(nextOperator) {//initialization of function handleOperator and pass a parametor
//destructing
const { firstOperand, displayValue, operator } = calculator;
const inputValue = parseFloat(displayValue);
if (operator && calculator.waitingForSecondOperand) {
calculator.operator = nextOperator;
return;
}
if (firstOperand == null && !isNaN(inputValue)) {
calculator.firstOperand = inputValue;
} else if (operator) {
const currentValue = firstOperand || 0;
const result = calculate(currentValue, inputValue, operator);
calculator.displayValue = `${parseFloat(result.toFixed(7))}`;
calculator.firstOperand = result;
}
calculator.waitingForSecondOperand = true;
calculator.operator = nextOperator;
}
// calculation
function calculate(firstOperand, secondOperand, operator) {
if (operator === '+') {
return firstOperand + secondOperand;
} else if (operator === '-') {
return firstOperand - secondOperand;
} else if (operator === '*') {
return firstOperand * secondOperand;
} else if (operator === '/') {
return firstOperand / secondOperand;
}
return secondOperand;
}
// Resetting our calc to defaults
function resetCalculator() {
calculator.displayValue = '0';
calculator.firstOperand = null;
calculator.waitingForSecondOperand = false;
calculator.operator = null;
}
//Update our calc display
function updateDisplay() {
const display = document.querySelector('.calculator-screen');
display.value = calculator.displayValue;
}
updateDisplay();
const keys = document.querySelector('.calculator-keys');
keys.addEventListener('click', event => {
const { target } = event;
const { value } = target;
if (!target.matches('button')) {
return;
}
switch (value) {
case '+':
case '-':
case '*':
case '/':
case '=':
handleOperator(value);
break;
case '.':
inputDecimal(value);
break;
case 'all-clear':
resetCalculator();
break;
default:
if (Number.isInteger(parseFloat(value))) {
inputDigit(value);
}
}
updateDisplay();
});