Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

131
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda