0 is causing calculation problem

I made this calculator and everything works fine but If I put 0 infront and then perform a calculation then it returns error which I cant figure out why Github: https://github.com/Pulkit-s21/calculatorThemes.git Live Site: https://calculator-themes-06ca1e.netlify.app/
GitHub
GitHub - Pulkit-s21/calculatorThemes
Contribute to Pulkit-s21/calculatorThemes development by creating an account on GitHub.
6 Replies
Sinc02
Sinc02•3y ago
It's probably because the value of display.value is not something that eval is expecting Try to console log what is display.value before putting it in eval function Maybe you can also try to put the return keyword right below line 139 in main.js. I assume that display.value has value of "Error" or something before it is passed to eval
MarkBoots
MarkBoots•3y ago
try and see where what actually is placing the error. you now have an if statement and the catch doing it. put some logs in. Also the return (what Sinc02 suggested) makes sense
case '=':
console.log(display.value);
if(display.value === ''){
console.log("empty string")
display.value = 'ERROR !';
return
}
try{
display.value = eval(display.value);
}catch(err){
console.log("catch error", err)
display.value = 'ERROR !';
}
break;
case '=':
console.log(display.value);
if(display.value === ''){
console.log("empty string")
display.value = 'ERROR !';
return
}
try{
display.value = eval(display.value);
}catch(err){
console.log("catch error", err)
display.value = 'ERROR !';
}
break;
Killer🔪
Killer🔪OP•3y ago
its treating the input as octal literal
MarkBoots
MarkBoots•3y ago
ah okay, then we'll need to trim of that 0;
const value = display.value.replace(/^0*/,""); //matches all zeroes at the start of the string, replaced by empty
const value = display.value.replace(/^0*/,""); //matches all zeroes at the start of the string, replaced by empty
place this just before the switch. and use value from then on
Killer🔪
Killer🔪OP•3y ago
I did it but its still giving the same error as before
Killer🔪
Killer🔪OP•3y ago
Want results from more Discord servers?
Add your server