works, but allows the first input to be an operator(+,-,*,/,=). How can I make the first input be number only?
const buttons = document.querySelectorAll('button');
const display = document.querySelector('.display');
added a variable to distinguish the operators
const operator = ['+', '-', '*', '/'];
buttons.forEach(function(button) {
button.addEventListener('click', calculate);
});
function calculate(event) {
const clickedButtonValue = event.target.value;
tried this, to no effect
if (display.value[0] = operator) {
display.value = '';
}
else if (clickedButtonValue === '=' && display.value !== '') {
display.value = eval(display.value)
} else if (clickedButtonValue === 'C') {
display.value = ''
} else {
display.value += clickedButtonValue;
}
}
if (display.value[0] === operator) { //changed your = operator to ===
display.value = '';
}
This should work, your problem was that you were assigning display.value[0] to operator instead of comparing them.