Skip to content
Open
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
54 changes: 29 additions & 25 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,70 +11,75 @@ let decimalAdded = false;

const operators = ["+", "-", "x", "÷"];

function handleKeyPress (e) {
function handleKeyPress(e) {
const key = e.target.dataset.key;
const lastChar = operation[operation.length - 1];

if (key === "=") {
return;
}
// Prevent action if the key is "="
if (key === "=") return;

if (key === "." && decimalAdded) {
return;
}
// Prevent multiple decimals in a number
if (key === "." && decimalAdded) return;

if (operators.indexOf(key) !== -1) {
// Reset decimalAdded flag when an operator is pressed
if (operators.includes(key)) {
decimalAdded = false;
}

// Allow leading minus sign for negative numbers
if (operation.length === 0 && key === "-") {
operation += key;
input.innerHTML = operation;
return;
}

if (operation.length === 0 && operators.indexOf(key) !== -1) {
// Prevent leading operators other than minus
if (operation.length === 0 && operators.includes(key)) {
input.innerHTML = operation;
return;
}

if (operators.indexOf(lastChar) !== -1 && operators.indexOf(key) !== -1) {
operation = operation.replace(/.$/, key);
// Replace the last operator with the new one if two operators are pressed consecutively
if (operators.includes(lastChar) && operators.includes(key)) {
operation = operation.slice(0, -1) + key;
input.innerHTML = operation;
return;
}

// Add the pressed key to the operation
if (key) {
if (key === ".") decimalAdded = true;
operation += key;
input.innerHTML = operation;
return;
}

}

function evaluate(e) {
function evaluateExpression(e) {
const key = e.target.dataset.key;
const lastChar = operation[operation.length - 1];

if (key === "=" && operators.indexOf(lastChar) !== -1) {
// Remove the last operator if "=" is pressed
if (key === "=" && operators.includes(lastChar)) {
operation = operation.slice(0, -1);
}

// Display empty result if no operation is present
if (operation.length === 0) {
answer = "";
result.innerHTML = answer;
return;
}

try {

// Remove leading zero unless it's a decimal number
if (operation[0] === "0" && operation[1] !== "." && operation.length > 1) {
operation = operation.slice(1);
}

const final = operation.replace(/x/g, "*").replace(/÷/g, "/");
answer = +(eval(final)).toFixed(5);
// Replace custom operators with JavaScript operators and evaluate
const finalExpression = operation.replace(/x/g, "*").replace(/÷/g, "/");
answer = +(eval(finalExpression)).toFixed(5);

if (key === "=") {
decimalAdded = false;
Expand All @@ -86,20 +91,18 @@ function evaluate(e) {
}

result.innerHTML = answer;

} catch (e) {
} catch (error) {
if (key === "=") {
decimalAdded = false;
input.innerHTML = `<span class="error">${operation}</span>`;
result.innerHTML = `<span class="error">Bad Expression</span>`;
}
console.log(e);
console.error(error);
}

}

function clearInput (e) {

function clearInput(e) {
// Clear all input if Ctrl key is pressed
if (e.ctrlKey) {
operation = "";
answer = "";
Expand All @@ -108,13 +111,14 @@ function clearInput (e) {
return;
}

// Remove the last character from the operation
operation = operation.slice(0, -1);
input.innerHTML = operation;

}

// Add event listeners
deleteBtn.addEventListener("click", clearInput);
keys.forEach(key => {
key.addEventListener("click", handleKeyPress);
key.addEventListener("click", evaluate);
key.addEventListener("click", evaluateExpression);
});