Improve code explanation
This commit is contained in:
parent
5eaad03e93
commit
4f4b3e377d
@ -7,6 +7,7 @@ let characterSpans = []; // Array to track all spans for proper positioning
|
|||||||
let isResultDisplayed = false; // Track if the result is displayed
|
let isResultDisplayed = false; // Track if the result is displayed
|
||||||
let resultSpan; // For showing the result
|
let resultSpan; // For showing the result
|
||||||
|
|
||||||
|
// Add on-click event listeners to the buttons.
|
||||||
buttons.forEach(button => {
|
buttons.forEach(button => {
|
||||||
button.addEventListener('click', () => {
|
button.addEventListener('click', () => {
|
||||||
const value = button.textContent;
|
const value = button.textContent;
|
||||||
@ -32,6 +33,7 @@ buttons.forEach(button => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// This function is used to append a character to the calculator display. It's called when user clicks a button.
|
||||||
function appendCharacter(char) {
|
function appendCharacter(char) {
|
||||||
// Create new span element for the character
|
// Create new span element for the character
|
||||||
const newChar = document.createElement('span');
|
const newChar = document.createElement('span');
|
||||||
@ -53,6 +55,7 @@ function appendCharacter(char) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// This function moves already input characters to left to make space for a new one.
|
||||||
function updateCharacterPositions() {
|
function updateCharacterPositions() {
|
||||||
const totalWidth = displayText.offsetWidth; // Width of the display
|
const totalWidth = displayText.offsetWidth; // Width of the display
|
||||||
let currentOffset = totalWidth; // Start from the rightmost edge
|
let currentOffset = totalWidth; // Start from the rightmost edge
|
||||||
@ -72,6 +75,7 @@ function updateCharacterPositions() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// This function calculates the result based on characters input.
|
||||||
function calculateResult() {
|
function calculateResult() {
|
||||||
const equation = characterSpans.map(span => span.textContent).join('');
|
const equation = characterSpans.map(span => span.textContent).join('');
|
||||||
let result;
|
let result;
|
||||||
@ -81,6 +85,8 @@ function calculateResult() {
|
|||||||
result = 'Error';
|
result = 'Error';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// We should add 'result' to the history here.
|
||||||
|
|
||||||
characterSpans.forEach(span => {
|
characterSpans.forEach(span => {
|
||||||
span.style.fontSize = '1.5rem';
|
span.style.fontSize = '1.5rem';
|
||||||
});
|
});
|
||||||
@ -123,6 +129,7 @@ function calculateResult() {
|
|||||||
isResultDisplayed = true;
|
isResultDisplayed = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// This function toggles the history menu.
|
||||||
function toggleHistory() {
|
function toggleHistory() {
|
||||||
$('body').toggleClass('history-open');
|
$('body').toggleClass('history-open');
|
||||||
|
|
||||||
@ -139,18 +146,14 @@ function toggleHistory() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// This function toggles the settings menu.
|
||||||
function toggleSettings() {
|
function toggleSettings() {
|
||||||
$('body').toggleClass('settings-open');
|
$('body').toggleClass('settings-open');
|
||||||
}
|
}
|
||||||
|
|
||||||
$('#history').on('click', toggleHistory);
|
$('#history').on('click', toggleHistory);
|
||||||
|
|
||||||
function clearDisplay() {
|
// Resets everything, the result and display.
|
||||||
// Remove all current characters (spans)
|
|
||||||
characterSpans.forEach(span => span.remove());
|
|
||||||
characterSpans = [];
|
|
||||||
}
|
|
||||||
|
|
||||||
function resetCalculator() {
|
function resetCalculator() {
|
||||||
currentInput = '';
|
currentInput = '';
|
||||||
characterSpans = [];
|
characterSpans = [];
|
||||||
@ -160,6 +163,7 @@ function resetCalculator() {
|
|||||||
|
|
||||||
// popLock (funny name) is true when a cbaracter pop animation is being progress.
|
// popLock (funny name) is true when a cbaracter pop animation is being progress.
|
||||||
let popLock = false;
|
let popLock = false;
|
||||||
|
// This function pops the last character input to the calculator.
|
||||||
function popCharacter() {
|
function popCharacter() {
|
||||||
if (popLock) return;
|
if (popLock) return;
|
||||||
popLock = true;
|
popLock = true;
|
||||||
@ -192,6 +196,8 @@ function popCharacter() {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// This sets the calculator display value to some string. I'm pretty sure it's not used, but I'll leave it here just in case.
|
||||||
function updateDisplay(value) {
|
function updateDisplay(value) {
|
||||||
// Clear existing spans and reset positions
|
// Clear existing spans and reset positions
|
||||||
displayText.innerHTML = '';
|
displayText.innerHTML = '';
|
||||||
|
@ -18,8 +18,12 @@ function applyUIColors() {
|
|||||||
setUIColors(background, text, buttonPrimary, buttonSecondary);
|
setUIColors(background, text, buttonPrimary, buttonSecondary);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// When the webpage loades
|
||||||
window.addEventListener('load', () => {
|
window.addEventListener('load', () => {
|
||||||
|
// Hide the loader
|
||||||
$('.loader').hide();
|
$('.loader').hide();
|
||||||
|
|
||||||
|
// Animate the calculator buttons appearing (with staggering applied).
|
||||||
anime({
|
anime({
|
||||||
targets: '.calculator button.button',
|
targets: '.calculator button.button',
|
||||||
opacity: [0, 1],
|
opacity: [0, 1],
|
||||||
|
Loading…
Reference in New Issue
Block a user