# Calculator, Need assistance [solved]

Calculator, Need assistance [solved]
0

#1

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 @ https://repl.it/@John_Nicole/BoldUnderstatedLoaderprogram

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. https://codepen.io/Mike-was-here123/pen/PQOYjB?editors=1010

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 repl.it.

On line 49 i send equation to the console, just as in repl.it, 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?

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

#2

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

#3

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.

``````      \$("#output").html(eval(equation))
``````

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.