You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
57 lines
1.3 KiB
57 lines
1.3 KiB
1 month ago
|
let currentInput = '';
|
||
|
let previousInput = '';
|
||
|
let operation = null;
|
||
|
|
||
|
function appendNumber(number) {
|
||
|
currentInput += number.toString();
|
||
|
document.getElementById('display').value = currentInput;
|
||
|
}
|
||
|
|
||
|
function appendDecimal(dot) {
|
||
|
if (!currentInput.includes(dot)) {
|
||
|
currentInput += dot;
|
||
|
document.getElementById('display').value = currentInput;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
function chooseOperation(op) {
|
||
|
if (currentInput !== '') {
|
||
|
previousInput = currentInput;
|
||
|
operation = op;
|
||
|
currentInput = '';
|
||
|
}
|
||
|
}
|
||
|
|
||
|
function calculateResult(equals) {
|
||
|
let result;
|
||
|
const prev = parseFloat(previousInput);
|
||
|
const curr = parseFloat(currentInput);
|
||
|
if (isNaN(prev) || isNaN(curr)) return;
|
||
|
switch (operation) {
|
||
|
case '+':
|
||
|
result = prev + curr;
|
||
|
break;
|
||
|
case '-':
|
||
|
result = prev - curr;
|
||
|
break;
|
||
|
case '*':
|
||
|
result = prev * curr;
|
||
|
break;
|
||
|
case '/':
|
||
|
result = prev / curr;
|
||
|
break;
|
||
|
default:
|
||
|
return;
|
||
|
}
|
||
|
currentInput = result.toString();
|
||
|
document.getElementById('display').value = currentInput;
|
||
|
previousInput = '';
|
||
|
operation = null;
|
||
|
}
|
||
|
|
||
|
function clearDisplay() {
|
||
|
currentInput = '';
|
||
|
previousInput = '';
|
||
|
operation = null;
|
||
|
document.getElementById('display').value = '';
|
||
|
}s
|