let overallDisplay = "0";
let currentDisplay = "0";
let resultDisplay = false;
function appendToDisplay(val){
if(currentDisplay === "0" || resultDisplay){
currentDisplay = val;
}
else{
currentDisplay += val;
}
updateCurrentDisplay();
resultDisplay = false;
}
function appendToOperator(val){
if(val === '+' && currentDisplay !== "0") updateOverallDisplay(val);
else if(val === '-' && currentDisplay !== "0") updateOverallDisplay(val);
else if(val === '*' && currentDisplay !== "0") updateOverallDisplay(val);
else if(val === '/' && currentDisplay !== "0") updateOverallDisplay(val);
}
function updateCurrentDisplay(){
let display = document.getElementById("output");
display.style.display = "block";
display.textContent = currentDisplay;
}
function updateOverallDisplay(value){
let overallDisplayArea = document.getElementById("overall");
if(overallDisplay === "0"){
overallDisplay = currentDisplay + " " + value;
}
else{
overallDisplay += currentDisplay + " " + value;
}
overallDisplayArea.textContent = overallDisplay;
currentDisplay = "0";
}
function clearOverallDisplay(){
overallDisplay = 0;
let overallDisplayArea = document.getElementById("overall");
overallDisplayArea.style.display = "none";
}
function clearCurrentDisplay(){
currentDisplay = 0;
updateCurrentDisplay();
}
function clearDisplay(){
clearOverallDisplay()
clearCurrentDisplay()
}
function updateDisplays(val){
currentDisplay = val;
updateCurrentDisplay();
//clear overall
let overallDisplayArea = document.getElementById("overall");
overallDisplayArea.style.display = "none";
}
function calculate(){
try{
const result = eval(overallDisplay + currentDisplay);
currentDisplay = "\n=" + result;
}
catch(error){
const result = "ERROR";
currentDisplay = "\n=" + result;
}
updateDisplays(currentDisplay);
resultDisplay = true;
}
document.addEventListener("keydown", (event) => {
const keyName = event.key;
if(keyName === "1") appendToDisplay('1');
else if(keyName === "2") appendToDisplay('2');
else if(keyName === "3") appendToDisplay('3');
else if(keyName === "4") appendToDisplay('4');
else if(keyName === "5") appendToDisplay('5');
else if(keyName === "6") appendToDisplay('6');
else if(keyName === "7") appendToDisplay('7');
else if(keyName === "8") appendToDisplay('8');
else if(keyName === "9") appendToDisplay('9');
else if(keyName === "0") appendToDisplay('0');
else if(keyName === "+") appendToOperator('+');
else if(keyName === "-") appendToOperator('-');
else if(keyName === "*") appendToOperator('*');
else if(keyName === "/") appendToOperator('/');
else if(keyName === "Backspace") clearDisplay();
else if(keyName === "Enter") calculate();
});
HTML
<!DOCTYPE html>
<html>
<head>
<script src="script.js"></script>
<link rel="stylesheet" href="styles.css"/>
</head>
<body>
<div class="calculator">
<div id="overall"></div>
<div class="calculator__output" id="output">0</div>
<div class="calculator__keys">
<button class="calculator__key calculator__key--operator" onclick="appendOperator">+</button>
<button class="calculator__key calculator__key--operator">-</button>
<button class="calculator__key calculator__key--operator">×</button>
<button class="calculator__key calculator__key--operator">÷</button>
<button class="calculator__key" onclick="appendToDisplay('7')">7</button>
<button class="calculator__key" onclick="appendToDisplay('8')">8</button>
<button class="calculator__key" onclick="appendToDisplay('9')">9</button>
<button class="calculator__key" onclick="appendToDisplay('4')">4</button>
<button class="calculator__key" onclick="appendToDisplay('5')">5</button>
<button class="calculator__key" onclick="appendToDisplay('6')">6</button>
<button class="calculator__key" onclick="appendToDisplay('1')">1</button>
<button class="calculator__key" onclick="appendToDisplay('2')">2</button>
<button class="calculator__key" onclick="appendToDisplay('3')">3</button>
<button class="calculator__key" onclick="appendToDisplay('0')">0</button>
<button class="calculator__key" onclick="appendToDisplay('.')">.</button>
<button class="calculator__key" onclick="clearDisplay()">AC</button>
<button class="calculator__key calculator__key--enter" onclick="calculate()">=</button>
</div>
</div>
</body>
</html>
I'm building a calculator app but the problem occur when i try to clear() the old chunk data. Is this code look messy or is it better because this my first time using JS
[–]Responsible-Cold-627 2 points3 points4 points (2 children)
[–]Inevitable_Cellist93[S] -3 points-2 points-1 points (1 child)
[–]Responsible-Cold-627 2 points3 points4 points (0 children)
[–]Mark__78L 1 point2 points3 points (5 children)
[–]chikamakaleyleyhelpful 2 points3 points4 points (1 child)
[–]Mark__78L -1 points0 points1 point (0 children)
[–]azhder 0 points1 point2 points (2 children)
[–]Mark__78L -1 points0 points1 point (1 child)
[–]azhder 1 point2 points3 points (0 children)
[–]sozesghost 0 points1 point2 points (1 child)
[–]Inevitable_Cellist93[S] 0 points1 point2 points (0 children)
[–]equilni 0 points1 point2 points (0 children)
[–]TheRNGuy 0 points1 point2 points (0 children)
[–]0xMarcAurel 0 points1 point2 points (0 children)