Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

129
Visualizações
Js beginner calculator. How to make the first string empty in case it's an operator

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;
}


}

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

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.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda