Calculator, Need assistance [solved]

I need help solving the equation in my calculator i am building. Before i show you it, heres something to keep in mind:

 var equation = 9+9
console.log(equation) // 18

You can further play with this @

That returns 18. I am trying to replicate this in my calculator. The issue is that it isn’t working when i try to do this.

You can skip through most of the code, line 42 is what code is executed when i press enter. I put some comments to help. Line 43 turns the array (equation) it into a string. The array had numbers, symbols pushed into it as you can see on lines 30 and 25.On line 45 it goes through and turns the numbers from strings to real numbers and can be confirmed on line 46 (check console). This is so i can try and replicate what i did in the

On line 49 i send equation to the console, just as in, with real numbers and symbols. Yet i get, for instance 9+9 instead of 18. My equation just isn’t solving like in the example, and if i check the type of equation it returns a string.

Why is it doing this?

Please ask for any clarification :slight_smile:

Oftopic: Having dual Monitors is pretty useful. While i’m waiting for replies i can do other stuff.

might be better if you can give me what line is doing the addition…

First things first - you’re trying to handle clicks twice - once in html and once in JS. Just do it in JS. Your buttons should look like:

            <li><button id="pi">π</button></li>

As to evaluating the equations in the console, it isn’t the same thing. If you type 9+9 into the console, it sees it as an expression and evaluates it. But in your JS, it isn’t an expression, it’s a string. It would be like if you typed "9+9" into the console, with the quotes. It’s just a string.

How do you get JS to evaluate a string? The quick way is with the eval function.


But be warned, there are some security concerns with eval. I don’t think it would be a problem here, but eval has a bad reputation. The other option would be to use a library that can parse and calculate the string or build your own.

1 Like