Here's a simple calculator with history functionality that you can use in a web browser. It keeps track of all calculations performed during the session.
html Copy code
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Calculator with History</title>
<style>
body {
font-family: Arial, sans-serif;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
background-color: #f5f5f5;
}
.calculator-container {
display: flex;
gap: 20px;
}
.calculator {
background-color: #fff;
border-radius: 10px;
box-shadow: 0 4px 10px rgba(0, 0, 0, 0.2);
padding: 20px;
width: 300px;
}
.history {
background-color: #fff;
border-radius: 10px;
box-shadow: 0 4px 10px rgba(0, 0, 0, 0.2);
padding: 20px;
width: 300px;
height: 400px;
overflow-y: auto;
}
.display {
background-color: #f8f8f8;
border: 1px solid #ddd;
border-radius: 5px;
padding: 10px;
margin-bottom: 15px;
text-align: right;
font-size: 24px;
height: 30px;
overflow: hidden;
}
.buttons {
display: grid;
grid-template-columns: repeat(4, 1fr);
gap: 10px;
}
button {
padding: 15px;
font-size: 18px;
border: none;
border-radius: 5px;
background-color: #e0e0e0;
cursor: pointer;
transition: background-color 0.2s;
}
button:hover {
background-color: #d0d0d0;
}
button.operator {
background-color: #ff9500;
color: white;
}
button.operator:hover {
background-color: #e68a00;
}
button.clear {
background-color: #ff3b30;
color: white;
}
button.clear:hover {
background-color: #e6352b;
}
button.equals {
background-color: #4cd964;
color: white;
}
button.equals:hover {
background-color: #44d25a;
}
.history-title {
text-align: center;
margin-bottom: 15px;
font-size: 20px;
font-weight: bold;
}
.history-item {
padding: 8px;
border-bottom: 1px solid #eee;
display: flex;
justify-content: space-between;
}
.history-item:last-child {
border-bottom: none;
}
.clear-history {
background-color: #ff3b30;
color: white;
width: 100%;
margin-top: 10px;
}
.clear-history:hover {
background-color: #e6352b;
}
</style>
</head>
<body>
<div class="calculator-container">
<div class="calculator">
<div class="display" id="display">0</div>
<div class="buttons">
<button class="clear" onclick="clearDisplay()">AC</button>
<button onclick="appendToDisplay('(')">(</button>
<button onclick="appendToDisplay(')')">)</button>
<button class="operator" onclick="appendToDisplay('/')">/</button>
<button onclick="appendToDisplay('7')">7</button>
<button onclick="appendToDisplay('8')">8</button>
<button onclick="appendToDisplay('9')">9</button>
<button class="operator" onclick="appendToDisplay('*')">×</button>
<button onclick="appendToDisplay('4')">4</button>
<button onclick="appendToDisplay('5')">5</button>
<button onclick="appendToDisplay('6')">6</button>
<button class="operator" onclick="appendToDisplay('-')">-</button>
<button onclick="appendToDisplay('1')">1</button>
<button onclick="appendToDisplay('2')">2</button>
<button onclick="appendToDisplay('3')">3</button>
<button class="operator" onclick="appendToDisplay('+')">+</button>
<button onclick="appendToDisplay('0')">0</button>
<button onclick="appendToDisplay('.')">.</button>
<button onclick="backspace()">⌫</button>
<button class="equals" onclick="calculate()">=</button>
</div>
</div>
<div class="history">
<div class="history-title">Calculation History</div>
<div id="history-list"></div>
<button class="clear-history" onclick="clearHistory()">Clear History</button>
</div>
</div>
<script>
let currentInput = '0';
let history = JSON.parse(localStorage.getItem('calculatorHistory')) || [];
function updateDisplay() {
document.getElementById('display').textContent = currentInput;
}
function appendToDisplay(value) {
if (currentInput === '0' && value !== '.') {
currentInput = value;
} else {
currentInput += value;
}
updateDisplay();
}
function clearDisplay() {
currentInput = '0';
updateDisplay();
}
function backspace() {
if (currentInput.length === 1) {
currentInput = '0';
} else {
currentInput = currentInput.slice(0, -1);
}
updateDisplay();
}
function calculate() {
try {
// Replace × with * for evaluation
const expression = currentInput.replace(/×/g, '*');
const result = eval(expression);
// Add to history
history.unshift({
expression: currentInput,
result: result,
timestamp: new Date().toLocaleString()
});
// Keep only the last 20 items
if (history.length > 20) {
history = history.slice(0, 20);
}
// Save to localStorage
localStorage.setItem('calculatorHistory', JSON.stringify(history));
// Update display
currentInput = result.toString();
updateDisplay();
updateHistory();
} catch (error) {
currentInput = 'Error';
updateDisplay();
}
}
function updateHistory() {
const historyList = document.getElementById('history-list');
historyList.innerHTML = '';
history.forEach(item => {
const historyItem = document.createElement('div');
historyItem.className = 'history-item';
historyItem.innerHTML = `
<span>${item.expression} =</span>
<span>${item.result}</span>
`;
historyList.appendChild(historyItem);
});
}
function clearHistory() {
history = [];
localStorage.removeItem('calculatorHistory');
updateHistory();
}
// Initialize
updateDisplay();
updateHistory();
</script>
</body>
</html>
To use this calculator, simply copy the entire code into an HTML file and open it in a web browser. The history will be saved between sessions as long as you don't clear your browser's local storage.