-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
113 lines (97 loc) · 3.13 KB
/
script.js
File metadata and controls
113 lines (97 loc) · 3.13 KB
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
// Hello, thanks for checking out my work. This is a simple calculator using JS
// based on different tutorials and my own code/logic
// for more info please refer to the read me file on GitHub
// declaration of variables
const calculator = {
outputValue: "0",
firstValue: null,
secondOperand: false,
operator: null,
};
// take the digits and compare
function inputDigit(digit) {
const { outputValue, secondOperand } = calculator;
if (secondOperand === true) {
calculator.outputValue = digit;
calculator.secondOperand = false;
} else {
calculator.outputValue = outputValue === "0" ? digit : outputValue + digit;
}
}
// decimal point
function inputDecimal(dot) {
if (calculator.secondOperand === true) return;
// if the outputValue does not contain a decimal point
if (!calculator.outputValue.includes(dot)) {
// append the decimal point
calculator.outputValue += dot;
}
}
// use parseFloat to convert string to integer
function handleOperator(nextOperator) {
const { firstValue, outputValue, operator } = calculator;
const inputValue = parseFloat(outputValue);
if (operator && calculator.secondOperand) {
calculator.operator = nextOperator;
return;
}
// perform calculation
if (firstValue == null) {
calculator.firstValue = inputValue;
} else if (operator) {
const currentValue = firstValue || 0;
const result = performCalculation[operator](currentValue, inputValue);
calculator.outputValue = String(result);
calculator.firstValue = result;
}
calculator.secondOperand = true;
calculator.operator = nextOperator;
}
const performCalculation = {
"/": (firstValue, secondOperand) => (firstValue / secondOperand).toFixed(3),
"*": (firstValue, secondOperand) => firstValue * secondOperand,
"+": (firstValue, secondOperand) => firstValue + secondOperand,
"-": (firstValue, secondOperand) => firstValue - secondOperand,
"=": (firstValue, secondOperand) => secondOperand,
};
// function to reset the calculator
function resetCalculator() {
calculator.outputValue = "0";
calculator.firstValue = null;
calculator.secondOperand = false;
calculator.operator = null;
}
//select the text area that displays the results using querySelector
function updateDisplay() {
const display = document.querySelector(".output-screen");
display.value = calculator.outputValue;
}
updateDisplay();
// select the buttons from DOM using querySelector and add EventListener
// confirm if a button was clicked and then fetch its action and values further
const keys = document.querySelector(".calculator-keys");
keys.addEventListener("click", (e) => {
const { target } = e;
if (!target.matches("button")) {
return;
}
if (target.classList.contains("operator")) {
handleOperator(target.value);
updateDisplay();
return;
}
// check if the displayed value already includes a decimal point
if (target.classList.contains("decimal")) {
inputDecimal(target.value);
updateDisplay();
return;
}
// reset the calculator
if (target.classList.contains("clear")) {
resetCalculator();
updateDisplay();
return;
}
inputDigit(target.value);
updateDisplay();
});