Skip to content

Commit 5097f52

Browse files
committed
update the script.js and main index.html
1 parent 3b241d5 commit 5097f52

File tree

2 files changed

+145
-9
lines changed

2 files changed

+145
-9
lines changed

04_calculator/script.js

Lines changed: 132 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,135 @@
1-
const displayEl = document.getElementById("display");
2-
const currentValueEl = document.getElementById("current-value");
3-
const buttonEl = document.querySelectorAll("button");
1+
const calculator = document.getElementById('calculator');
2+
const display = document.getElementById('display');
43

5-
const clear = document.querySelector('[data-type = "clear"]');
6-
const utility = document.querySelector('[data-type="utility"]');
7-
const operator = document.querySelectorAll('[data-type="operator"]');
8-
const number = document.querySelectorAll('[data-type="number"]');
9-
const decimal = document.querySelector('[data-type="decimal"]');
10-
const calculate = document.querySelector('[data-type = "calculate"]');
4+
let firstOperand = null;
5+
let secondOperand = false;
6+
let operator = null;
7+
let currentInput = '0';
118

9+
function updateDisplay() {
10+
    if (currentInput.length > 10) {
11+
        const num = parseFloat(currentInput);
12+
        display.textContent = num.toPrecision(10);
13+
    } else {
14+
        display.textContent = currentInput;
15+
    }
16+
}
17+
function calculate(n1, n2, op) {
18+
    const num1 = parseFloat(n1);
19+
    const num2 = parseFloat(n2);
1220

21+
    if (isNaN(num1) || isNaN(num2)) return NaN;
22+
23+
    switch(op) {
24+
        case 'add':
25+
            return num1 + num2;
26+
        case 'subtract':
27+
            return num1-num2;
28+
        case 'multiply':
29+
            return num1 * num2;
30+
        case 'divide':
31+
            if (num2 === 0) {
32+
                return 'Error';
33+
            } else {
34+
                return num1 / num2;
35+
            }
36+
        default:
37+
            return num2;
38+
    }
39+
}
40+
41+
calculator.addEventListener('click', (event) => {
42+
    if (!event.target.matches('.btn')) return;
43+
44+
    const button = event.target;
45+
    const value = button.dataset.value;
46+
    const action = button.dataset.action;
47+
48+
    if (value && !action) {
49+
        if(secondOperand === true) {
50+
            currentInput = value;
51+
            secondOperand = false;
52+
        } else {
53+
            currentInput = currentInput === '0' ? value : currentInput + value;
54+
        }
55+
        updateDisplay();
56+
        return;
57+
    }
58+
59+
    if (action === 'decimal') {
60+
        if (secondOperand === true) {
61+
            currentInput = '0.';
62+
            secondOperand = false;
63+
        } else if (!currentInput.includes('.')) {
64+
            currentInput += '.';
65+
        }
66+
        updateDisplay();
67+
        return;
68+
    }
69+
70+
    if (action === 'clear') {
71+
        firstOperand = null;
72+
        operator = null;
73+
        secondOperand = false;
74+
        currentInput = '0';
75+
        updateDisplay();
76+
        return;
77+
    }
78+
79+
    if (action === 'sign') {
80+
        currentInput = String(parseFloat(currentInput) * -1);
81+
        updateDisplay();
82+
        return;
83+
    }
84+
    if (action === 'percent') {
85+
        currentInput = String(parseFloat(currentInput) / 100);
86+
        updateDisplay();
87+
        return;
88+
    }
89+
90+
    if (action === 'operator') {
91+
        const inputNumber = currentInput;
92+
93+
        if(firstOperand === null) {
94+
            firstOperand = inputNumber;
95+
        } else if(operator) {
96+
            const result = calculate(firstOperand, inputNumber, operator);
97+
           
98+
            if(result === 'Error') {
99+
                currentInput = 'Error';
100+
                firstOperand = null;
101+
                operator = null;
102+
            } else {
103+
                currentInput = String(result);
104+
                firstOperand = currentInput;
105+
            }
106+
        }
107+
        operator = value;
108+
        secondOperand = true;
109+
        updateDisplay();
110+
        return;
111+
    }
112+
113+
    if (action === 'calculate') {
114+
        if (firstOperand === null || operator === null) {
115+
            return;
116+
        }
117+
       
118+
        const inputNumber = currentInput;
119+
        const result = calculate(firstOperand, inputNumber, operator);
120+
       
121+
        if (result === 'Error') {
122+
            currentInput = 'Error';
123+
        } else {
124+
            currentInput = String(result);
125+
        }
126+
127+
        firstOperand = null;
128+
        operator = null;
129+
        secondOperand = true;
130+
        updateDisplay();
131+
        return;
132+
    }
133+
});
134+
135+
updateDisplay();

index.html

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,19 @@ <h2 class="text-lg font-medium uppercase tracking-widest text-gray-700 mb-6">
6262
</span>
6363
</a>
6464
</li>
65+
66+
<li>
67+
<a href="04_calculator/"
68+
class="flex justify-between items-center border border-gray-200 p-4
69+
hover:bg-black hover:text-white transition-colors duration-200 group">
70+
<span class="text-xl font-semibold group-hover:underline">
71+
04. Calculator App
72+
</span>
73+
<span class="text-sm uppercase font-mono tracking-widest text-gray-500 group-hover:text-white transition-colors">
74+
Event Delegation, Input Handling
75+
</span>
76+
</a>
77+
</li>
6578
</ul>
6679

6780
<footer class="mt-20 text-center text-xs text-gray-400">

0 commit comments

Comments
 (0)