I’m doing the calculator project and I have a problem. The input field value is a string eg '1+4/59’ etc. How do I calculate inside a string or if not what do I do instead.
eg var x = '1+4/5
9’
How do I make x = '8.2’
So what I’m asking is, how do I calculate as if it wasn’t a string

1 Like

You can use `eval()`.

``````var x = "1+4/5*9";
var answer = eval(x);    // 8.2
``````
2 Likes

Thank you so much. It worked like a charm.

You might want to use Number().

For ex:

``````var a = "72" // typeof a => string
var b = Number(a); // typeof b = > number
``````

That being said:

``````var a = "2";
var b = "3";
console.log(a * b) ; // 6
console.log(a + b); //  23 (no coercion so just strings);
``````

Remember that I want to calculate numbers within the same string. Number doesn’t cater to that.
eg Number(‘1+4/5*9’) will return NaN.
Thank you for the suggestion.

Damn. I’m such a … and I used eval() too but didn’t think of it

1 Like

first off i suggest you create your calculator using this link below

they are many different ways of doing the calculation … everybody probably did it differently

so i would suggest looking at the code of those created already to get a few ideas (we can now look at code to help us … just dont copy)

I dont recommend using eval

1 Like

console.log(a+b) is 23 … string + string is a string

yes, that’s what console.log shows though my syntax may be funky. o shit, typo !!!

here just to make it more fun console.log(a++ + b++);

Right ! I remember having seen that - was it in YDKJS ?

yep lol … enjoyed those books learnt some interesting stuff from them

Back when I did this project, my code would make an array that looked like this :

`[ '1', '+' , '4', '/', '5', '*', '9' ]`

Then I used 2 loops, the first to compute multiply and divide operations(replace ‘4’, ‘/’, and ‘5’ with 0.8), and the 2nd for plus and minus.It was a very basic calculator though (no complex stuff, not even parenthesis).

My calculator already works. What’s wrong with using eval()?

This is what MDN say about eval() … so its better to learn about this function before you decide to use it … and then only use it with care.

eval() is a dangerous function, which executes the code it’s passed with the privileges of the caller. If you run eval() with a string that could be affected by a malicious party, you may end up running malicious code on the user’s machine with the permissions of your webpage / extension. More importantly, third party code can see the scope in which eval() was invoked, which can lead to possible attacks in ways to which the similar Function is not susceptible.

eval() is also generally slower than the alternatives, since it has to invoke the JS interpreter, while many other constructs are optimized by modern JS engines.

There are safer (and faster!) alternatives to eval() for common use-cases.

2 Likes

Thank you
I like these guidelines I’ve seen flaws in the calculator now. I’ll work on them.

1 Like

I think it would be safe enough if you parse the string given to eval() using regexp and filter out anything that is not a digit, +, - /, * ( or ), would it be not?

1 Like