Skip to content
Open
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
let displayValue = "";
let memory = 0;
let history = [];

function appendNumber(number) {
displayValue += number;
updateDisplay();
}

function appendOperator(operator) {
if (displayValue === "") return;
if (isNaN(displayValue[displayValue.length - 1])) return;
displayValue += operator;
updateDisplay();
}

function appendFunction(func) {
displayValue = `${func}(${displayValue})`;
calculate();
}

function clearDisplay() {
displayValue = "";
updateDisplay();
}

function deleteLast() {
displayValue = displayValue.slice(0, -1);
updateDisplay();
}

function memoryStore() {
memory = parseFloat(displayValue) || 0;
updateDisplay();
}

function memoryRecall() {
displayValue += memory.toString();
updateDisplay();
}

function updateDisplay() {
document.getElementById("display").value = displayValue;
}

function calculate() {
try {
const result = eval(displayValue);
history.push(`${displayValue} = ${result}`);
displayValue = result.toString();
updateDisplay();
updateHistory();
} catch (error) {
displayValue = "Error";
updateDisplay();
setTimeout(clearDisplay, 1500);
}
}

function updateHistory() {
document.getElementById("history").textContent = history[history.length - 1] || "";
}

// Keyboard support
document.addEventListener("keydown", function (e) {
if (!isNaN(e.key)) appendNumber(e.key);
else if (['+', '-', '*', '/'].includes(e.key)) appendOperator(e.key);
else if (e.key === 'Enter') calculate();
else if (e.key === 'Backspace') deleteLast();
else if (e.key === 'Escape') clearDisplay();
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Advanced Calculator</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="calculator">
<input type="text" id="display" disabled placeholder="0">
<div id="history"></div>

<div class="buttons">
<button onclick="clearDisplay()">C</button>
<button onclick="deleteLast()">⌫</button>
<button onclick="memoryStore()">MS</button>
<button onclick="memoryRecall()">MR</button>
<button onclick="appendOperator('/')">/</button>
<button onclick="appendNumber('7')">7</button>
<button onclick="appendNumber('8')">8</button>
<button onclick="appendNumber('9')">9</button>
<button onclick="appendOperator('*')">x</button>
<button onclick="appendNumber('4')">4</button>
<button onclick="appendNumber('5')">5</button>
<button onclick="appendNumber('6')">6</button>
<button onclick="appendOperator('-')">-</button>
<button onclick="appendNumber('1')">1</button>
<button onclick="appendNumber('2')">2</button>
<button onclick="appendNumber('3')">3</button>
<button onclick="appendOperator('+')">+</button>
<button onclick="appendNumber('0')">0</button>
<button onclick="appendNumber('.')">.</button>
<button onclick="calculate()">=</button>

<!-- Scientific functions -->
<button onclick="appendFunction('Math.sqrt')">√</button>
<button onclick="appendFunction('Math.sin')">sin</button>
<button onclick="appendFunction('Math.cos')">cos</button>
<button onclick="appendFunction('Math.tan')">tan</button>
<button onclick="appendFunction('Math.log')">ln</button>
<button onclick="appendFunction('Math.exp')">e^</button>
</div>
</div>
<script src="app.js"></script>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
body {
font-family: Arial, sans-serif;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background-color: #f4f4f9;
margin: 0;
}

.calculator {
width: 300px;
background-color: #333;
border-radius: 10px;
box-shadow: 0px 0px 15px rgba(0, 0, 0, 0.2);
padding: 20px;
color: #fff;
text-align: center;
}

#display {
width: calc(100% - 20px); /* Added margin inside display */
height: 50px;
font-size: 1.5rem;
text-align: right;
padding: 10px 15px; /* Extra padding for spacing */
margin-bottom: 15px; /* Increased margin for separation */
border-radius: 5px;
border: none;
background-color: #222;
color: white;
}

#history {
font-size: 0.9rem;
color: #aaa;
margin-bottom: 10px;
height: 20px;
}

.buttons {
display: grid;
grid-template-columns: repeat(4, 1fr);
gap: 10px;
}

button {
width: 100%;
padding: 15px;
font-size: 1rem;
background-color: #555;
color: white;
border: none;
border-radius: 5px;
cursor: pointer;
}

button:hover {
background-color: #666;
}

button:active {
background-color: #444;
}

button:nth-child(5n+4),
button:nth-child(5n+5) {
background-color: #ff8c00;
color: white;
}
Loading