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

163
Visualizações
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 à 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