Untitled

mail@pastecode.io avatar
unknown
plain_text
7 months ago
3.8 kB
3
Indexable
Never
const screen = document.getElementById('screen');
const buttons = document.getElementsByClassName('numbers');
const btnC = document.getElementsByName('btn-C')[0];
const btnCE = document.getElementsByName('btn-CE')[0];
const btnNeg = document.getElementsByName('btnNeg')[0];
const btnPercent = document.getElementsByName('btnPercent')[0];
const btnPlus = document.getElementsByName('btn-Plus')[0];
const btnMinus = document.getElementsByName('btn-Minus')[0];
const btnMultiply = document.getElementsByName('btn-Multiply')[0];
const btnDivide = document.getElementsByName('btn-Divide')[0];
const btnDecimal = document.getElementsByName('btn-Decimal')[0];
const btnEquals = document.getElementsByName('btn-Equals')[0];
const operators = ['+', '-', '*', '/'];
let checkPressOperator = false;
let prev = null;
let current = null;
let total = null;
let currentOperator = null;

// Attach button click events for numbers
for (let i = 0; i < buttons.length; i++) {
  buttons[i].addEventListener('click', function() {
    appendValue(buttons[i].value.trim());
  });
}

// Attach click event for "C" button
btnC.addEventListener('click', clearScreen);

// Attach click event for "CE" button
btnCE.addEventListener('click', clearEntry);

// Attach click event for "+/-" button
btnNeg.addEventListener('click', negate);

// Attach click event for "%" button
btnPercent.addEventListener('click', calculatePercentage);

// Attach click event for "+" button
btnPlus.addEventListener('click', function() {
  setOperator('+');
});

// Attach click event for "-" button
btnMinus.addEventListener('click', function() {
  setOperator('-');
});

// Attach click event for "*" button
btnMultiply.addEventListener('click', function() {
  setOperator('*');
});

// Attach click event for "/" button
btnDivide.addEventListener('click', function() {
  setOperator('/');
});

// Attach click event for "." button
btnDecimal.addEventListener('click', function() {
  appendDecimal();
});

// Attach click event for "=" button
btnEquals.addEventListener('click', calculate);

// Clear the screen
function clearScreen() {
  screen.value = '0';
  prev = null;
  current = null;
  total = null;
  currentOperator = null;
}

// Clear the last entry
function clearEntry() {
  if (screen.value !== '0') {
    screen.value = screen.value.slice(0, -1);
  }
}

// Append a value to the screen
function appendValue(value) {
  if (screen.value === '0') {
    screen.value = value;
  } else {
    if (checkPressOperator) {
        screen.value = "";
        checkPressOperator = false;
    }
    screen.value += value;
  }
}

// Append a decimal point to the screen
function appendDecimal() {
  if (!screen.value.includes('.')) {
    screen.value += '.';
  }
}

// Negate the current value on the screen
function negate() {
  screen.value = -parseFloat(screen.value)
}

// Calculate the percentage of the current value on the screen
function calculatePercentage() {
  screen.value = eval(screen.value + '/100');
}

// Set the operator for calculation
function setOperator(operator) {
    checkPressOperator = true
    currentOperator = operator
    prev = screen.value
}

// Perform the calculation
function calculate() {
    current = parseFloat(screen.value)
    prev = parseFloat(prev)
    switch(currentOperator) {
        case '+':
            screen.value = prev + current
            break
        case '-':
            screen.value = prev - current
            break
        case '*':
            screen.value = prev * current
            break
        case '/':
            screen.value = prev / current
            break
    }
    total = screen.value
    currentOperator = null
    prev = current
    
}