+ 2
23rd May 2021, 4:35 PM
Harsh Mandwariya
Harsh Mandwariya - avatar
4 Answers
+ 4
because you're handling operators with eval(), so when you click on an operator, you're trying to eval() an unfinished equation ^^ you can fix it by moving the eval() call in the '=' operator handler, and delete the result field in other operator handlers: else if(d =='='){ v = eval(equation) result.innerText=v before.innerText =v equation=v } else{ equation+=d before.innerText = equation // v = eval(equation) result.innerText = "" // v }})
23rd May 2021, 5:57 PM
visph
visph - avatar
+ 4
when you use your calculator, you first type some numbers wich are appended to your 'equation' variable, then you type an operator, other than 'c', 'ce' and '='... so your old code goes to the 'else' statement where you append it to the 'equation' variable ' wich you tried to eval() but that string look like "40+" wich has an unexpected end of input while parsing the expression ;) the fix I suggested, remove this eval() call that cause the error and clear the display to be ready to show next numbers typed... then, you can continue the expression by typing again some numbers wich are appended to the 'expression' variable, until you type '=', where you can eval() safely (as you doesn't append to it the '=') and display result ^^ however, I've only fixed the (blocking) error that you asked... you could improve code by handling user mistakes: + expression start with operator + consecutive operators without number in between + result of partial expression to allow more than one operator before '=' + ...
24th May 2021, 5:04 AM
visph
visph - avatar
0
visph I request you to explain me more further. I understood your words but I can't understand the logic.
24th May 2021, 4:35 AM
Harsh Mandwariya
Harsh Mandwariya - avatar
0
visph omg you explained in such a great manner sir
24th May 2021, 11:19 AM
Harsh Mandwariya
Harsh Mandwariya - avatar