Skip to content
Open

js #3

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

<body>
<div class="calculator">
<div class="display">&nbsp;</div>
<div class="display"></div>
<div class="buttons">
<div class="row">
<button class="number" id="number-7">7</button>
Expand All @@ -28,10 +28,10 @@
<button class="number" id="number-3">3</button>
</div>
<div class="row operations">
<!-- <button id="add">+</button>
<button id="add">+</button>
<button id="subtract">-</button>
<button id="multiply">*</button>
<button id="divide">/</button> -->
<button id="divide">/</button>
</div>
<div class="row last-row">
<button id="clear">C</button>
Expand All @@ -43,4 +43,5 @@
<script src="script.js"></script>
</body>

</html>
</html>

95 changes: 95 additions & 0 deletions script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
const display = document.querySelector('.display');
const numberButtons = document.querySelectorAll('.number');
const clearButton = document.querySelector('#clear');

const addOp = document.getElementById('add');
const subtractOp = document.getElementById('subtract');
const divideOp = document.getElementById('divide');
const multiplyOp = document.getElementById('multiply');
const equalButton = document.getElementById('equal');

numberButtons.forEach((button) => {
const value = button.innerText;
button.addEventListener('click', () => {
if (display.innerText === '') {
display.innerText = value;
} else {
display.innerText += value;
}
});
});

clearButton.addEventListener('click', () => {
display.innerText = '';
});

addOp.addEventListener('click', () => {
display.innerText += '+';
});

subtractOp.addEventListener('click', () => {
display.innerText += '-';
});

divideOp.addEventListener('click', () => {
display.innerText += '/';
});

multiplyOp.addEventListener('click', () => {
display.innerText += '*';
});

equalButton.addEventListener('click', () => {
const expression = display.innerText;
try {
const result = calc(expression);
display.innerText = result;
} catch (e) {
display.innerText = '';
}
});

function calc(expression) {
const operators = ['+', '-', '*', '/'];
let op = null;
let opIndex = -1;

for (const candidate of operators) {
const idx = expression.indexOf(candidate);
if (idx > 0) {
op = candidate;
opIndex = idx;
break;
}
}

if (op === null || opIndex === -1 || opIndex === expression.length - 1) {
throw new Error('Invalid expression');
}

const left = expression.slice(0, opIndex);
const right = expression.slice(opIndex + 1);

const a = Number(left);
const b = Number(right);

if (Number.isNaN(a) || Number.isNaN(b)) {
throw new Error('Invalid number');
}

switch (op) {
case '+':
return a + b;
case '-':
return a - b;
case '*':
return a * b;
case '/':
if (b === 0) {
throw new Error('Division by zero');
}
return a / b;
default:
throw new Error('Unknown operator');
}
}
15 changes: 7 additions & 8 deletions style.css
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
--display-bg: #1f2937;
--display-color: #ffffff;
--text-color: #111827;
--success-bg: #10b981;
--danger-bg: #ef4444;
}

* {
Expand Down Expand Up @@ -40,13 +42,12 @@ body {
background-color: var(--display-bg);
color: var(--display-color);
font-size: 2.5rem;
padding: 20px;
padding: 40px;
border-radius: 10px;
text-align: right;
text-align: left;
margin-bottom: 10px;
overflow-x: auto;
}

.buttons {
display: flex;
flex-direction: column;
Expand Down Expand Up @@ -81,7 +82,6 @@ button:active {
transform: translateY(0);
}

/* Operations Row Styling */
.operations button {
background-color: var(--op-bg);
color: var(--op-color);
Expand All @@ -92,14 +92,13 @@ button:active {
filter: brightness(110%);
}

/* Last Row Styling */
.last-row button {
background-color: #10b981; /* Green for equals/action */
color: white;
background-color: var(--success-bg);
color: var(--op-color);
}

.last-row button:first-child {
background-color: #ef4444; /* Red for Clear */
background-color: var(--danger-bg);
}

.last-row button:nth-child(2) {
Expand Down