Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

166
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!