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!