+ 2
Why this code is showing unexpected end of input
4 Réponses
+ 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
}})
+ 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 '='
+ ...
0
visph I request you to explain me more further. I understood your words but I can't understand the logic.
0
visph omg you explained in such a great manner sir