za mitito

mail@pastecode.io avatar
unknown
javascript
2 years ago
2.1 kB
2
Indexable
Never
const input = document.querySelector('.input');
const buttons = document.querySelectorAll('.action');
const calculationList = document.querySelector('.calculation-list');
const clearButton = document.querySelector('.clear');

let currentValue = '';
let previousValue = '';
let operation = '';

buttons.forEach(button => {
  button.addEventListener('click', () => {
    const value = button.innerText;

    if (!isNaN(value)) {
      currentValue += value;
      input.value = currentValue;
    } else if (value === 'C') {
      currentValue = '';
      input.value = currentValue;
    } else if (value === '=') {

      const result = eval(previousValue + operation + currentValue);
      input.value = result;
      let temp1 = currentValue;
      let temp2 = previousValue;
      currentValue = result;

      const calculation = document.createElement('li');
      calculation.innerText = `${temp1} ${operation} ${temp2} = ${result}`;
      calculationList.appendChild(calculation);
    }else if (value === 'sqrt') {
        input.value = Math.sqrt(currentValue);
        let temp = currentValue;
        currentValue = input.value;
        const calculation = document.createElement('li');
        calculation.innerText = `${`√`}${temp} = ${input.value}`;
        calculationList.appendChild(calculation);
        // previousValue = '';
        // operation = '';
    }else if (value === 'pow') {
            input.value = Math.pow(currentValue, 2);
            let temp = currentValue;
            currentValue = input.value;
            const calculation = document.createElement('li');
            calculation.innerText = `${temp}${`^2`} = ${input.value}`;
            calculationList.appendChild(calculation);
    } else {
      previousValue = currentValue;
      operation = value;
      currentValue = '';

      button.style.border = '1px solid black';
      setTimeout(() => {
        button.style.border = '';
      }, 200);
    }
  });
});

clearButton.addEventListener('click', () => {
  calculationList.innerHTML = '';
});