# Build a Javascript Calculator - evaluating expression help

Hello!

I’m wondering if anyone can point me in the right direction with respect to how to parse & complete the calculations, based on the data in entered into the calculator…

For instance, once I hit equals, I’m at a point where I’m evaluating a string which looks as follows:
“33+4*2/6”

Now, the eval() method seems to work. But the internet says it’s deprecated and unsafe… I also found the math.js library which can easily do the math… But if I wanted to evaluate that string myself and complete the calculations, can someone point me into the direction to get started?

I’ve spent a few hours messing around with it. I split the string into an array and managed to find a regex that would also keep the separators (math operators) in the array as well… But I have no idea how to evaluate that now…
Am I missing a Math method which makes this easier or something? I think I’m making this harder than it has to be.

So you are able to convert this to an array that looks something like the following?

``````['33', '+', '4', '*', '2', '/', '6']
``````

If so, then it seems like you should be able to go through it an item at a time, keeping a running total, and perform the necessary operations on that total as you encounter them. For example, your total is going to start out at `33` since that’s the first number in the array. Then you are going to add something to that total since the next item is the plus sign. Then you find out you’ll add `4` to the total since the next item is a `4`. You will of course have to figure out how to remember what you want to do when you encounter a new number. And you should be able to handle multiple operators in a row, such as `['10', '+', '-', '5']`.

Yes, exactly. I have it down to `['33', '+', '4', '*', '2', '/', '6']` form.

What I was initially trying to do was do the dividing/multiplying first, to follow standard order of operations. But, maybe I should just do it left to right and save myself a bit of the hassle right now.

Going to take a break for a couple hours and come back to it, has me a little frustrated at the moment! Thanks Mr Smooth.

Left to right would definitely be a little easier. Maybe start off with that and then once you have a better understanding of how it works you could update to use “formula” logic.

Good grief I’m an idiot, I was making this much too complicated.

Instead of just using a running total, as you said, I was trying to create a function to find the mathematical operators inside the array, then have it complete the interaction with the value before and after it, create a new array with the product of said operation, then run the formula again searching for another operator, it was a total mess.

5 hours of pounding my head on the wall and the solution was insanely simple.

Thanks Mr Smooth.

1 Like

Nope. Trust me, you are not the only person who has ever spent a bunch of time on something and then suddenly realizes the solution was a simple alternative. Happens to all of us.