Untitled
unknown
plain_text
6 months ago
1.6 kB
2
Indexable
let currentInput = ''; let operator = ''; let previousInput = ''; let equation = ''; let last_operator = ''; const hasil = document.getElementsByClassName('panel-hasil')[0]; const buttons = document.querySelectorAll('.baris'); buttons.forEach(button => { button.addEventListener('click', function() { const value = this.getAttribute('data-value'); if (value >= '0' && value <= '9' || value === '.') { if(last_operator !== "=") { equation += value; currentInput += value; }else{ equation = value; currentInput = value; } hasil.innerText = equation; } else if (value === 'X' || value === 'divide' || value === '+' || value === '-' ) { if (currentInput) { operator = value === 'X' ? '*' : (value === 'divide' ? '/' : value); previousInput = currentInput; currentInput = ''; equation += value === 'X' ? 'x' : (value === 'divide' ? '÷' : value); hasil.innerText = equation; } } else if (value === '=') { if (previousInput && currentInput) { let result = eval(previousInput + operator + currentInput); // hasil.innerText = equation + "=" + result; hasil.innerText = result; currentInput = result.toString(); previousInput = ''; operator = ''; equation = currentInput; } } last_operator = value; }); });
Editor is loading...
Leave a Comment