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

164
Vistas
Why javascript calculator don't show correct operator?

I am at the very beginning of the learning process of web development. I'm doing the odin project and I am at the task what wants me to make a basic calculator.

I have figured out many things and made it work but if the user adds a half expression like 3+ and after this press the -, *, or / buttons the display shows NaN. It is bad.

I'd like that if the user puts in for example 3+ and after it he press the - button the display should show 3- and make a substract after the second value added.

I cannot figure it out now.. but I'm sure it is trivial..

Here is my project:

https://github.com/JohnyKidd/calculator

Here is the code snippet which may be the problem. This is where I add values to the display. It is the same for all four basic operations:

plusButton.addEventListener("click", function(e){
  if (display.value.includes("+")||display.value.includes("-")||display.value.includes("*")||display.value.includes("/")){
    eval = display.value.split("+");
    console.log(eval);
    for(let i=0;i<eval.length;i++){
      eval[i]=parseFloat(eval[i]);
      result=eval[0]+eval[1];
    }
    console.log(eval);
    display.value=result;
    result=0;
  }
  else {
    display.value+="+";
  }
});

Sorry for my messy code. I know it must be improved. I will try to improve myself.

Thanks for any advice!

about 4 years ago · Juan Pablo Isaza
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