0
How can I get my equal button on the calculator to return the result?
https://code.sololearn.com/WWsM8jajToBX/?ref=app I changed my calculator to eval the string without parsing but it still returns undefined. Iāve tried a bunch of other things too. The last answer I had to this question was helpful but Iām just not sure what Iām doing wrong here. Any help would be greatly appreciated.
3 Answers
+ 3
Thomas
At line 18 (z = eval(output[0].innerHTML);) here you are trying to evaluate nothing meaning that output[0].innerHTML is equal to nothing(you can use alert(output[0].innerHTML) to see for yourself) because of this z is undefined.
The nothingness of output[0].innerHTML is due to the nu() where output[0].innerHTML was cleared. You should have saved the contents of the display before clearing it. Then evaluate the saved contents to display the results.
https://code.sololearn.com/WEbwH9FM08KH/#js
+ 1
Wait...
+ 1
Here's an idea for the equal button on click function:
Get the text from the screen
Convert it into int like this: parseInt(textbox.value);
Then evaluate using eval() function (This is not efficient though but it does the work).
Like this: eval(parseInt(textbox.value));
I am 100% sure this will return the result of the equation.