Addition to Calculator: Ability to use backspace + click keys on keyboard
// Listen for keyboard events document.addEventListener(“keydown”, function(event) { // Check which key was pressed const key = event.key;
// Handle numbers (0-9) and decimal point if (/^[0-9]$/.test(key) || key === “.”) { number(key); }
// Handle Backspace key for delete if (key === “Backspace”) { deleteLastCharacter(); } });
// Function to delete the last character function deleteLastCharacter() { const currentOutput = output.innerHTML; if (currentOutput.length > 1) { output.innerHTML = currentOutput.slice(0, -1); } else { output.innerHTML = “0”; nextReady = true; } }
Explanation
This code listens for certain key clicks. This code gives function to the 0-9 keys, the decimal, and backspace. The decimal and number keys are the easiest because you just need to lsiten for it and then input it into the calculator. The backspace is harder because it requires you to give the key a function that isn’t as straight forward as numbers.
Problem I Faced
When viewing my github pages, many of my notebooks were unavailable. So a simple, yet hard to find fix was going into the github repository and opening one of the notebook files. This easy yet effective fix was something that I stumbled upon trying to investigate the issue.