Help with calculator - performing the arithmetic

I have a basic Calc design - working more on the functionality right now-----

I am trying really hard to not look at any source code or watch tutorials on building a calculator…

I am able to at the numbers and arithmetic operators…

1- How do i take the contents of the

to perform the arithmetic???

1 Like

The eval() function is what you want. It will return any math you give it.

eval(2 * 3); // Returns 6

Do understand that this should be the only time you use eval(). It’s a very dangerous function, and it’s best to avoid it most of the time. For this small project, it’s easier than trying to implement the arithmetic yourself.

3 Likes

After I append(). Number and arithmetic operators to the div element class=input- the console log shows a blank when I do $(’.input’).val()

Try $('.input').html() instead.

Thanks I forgot .val(). Is for input elements…

I didn’t know for eval() and I did differently :frowning:(

it works but now I feel I miss something. I have to much code maybe ?

I reinvented the wheel :frowning:

It is what it is, man. These are all just practice projects, so don’t stress out about them too much.

1 Like

I’m not stress but I spent 3 days on the calculator and I’m trying to finish FCC with a nice stuff.
Here I just reinvented eval() I think is not smart at all and it is a frustration too…

This is really clever :smiley: But implementing the shunting yard algorithm for this project is quite fun too :slight_smile:

1 Like

I don’t really understand why is a bad idea to use eval() ?

I researched eva()…

Don’t use eval needlessly!EDIT
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.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/eval

Shunting yard ALGORITHMS looks advance… can I have a link to your codepen - calculator

Here’s my calculator.

To make more sense of things, read about it on Wikipedia.

1 Like

@kevcomedia1h

nice but you don’t really need Jquery, I really like it.
You produce a lot of code for this little calculator 200 lines, but why not.

What is ‘shunting yard algorithm’ ?

It converts an infix expression (like 2 * 3) into a postfix expression (like 2 3 *),
or (2 + 6) / 4 into 2 6 + 4 /. It looks weird but it’s easy to evaluate from left to right, but that’s a different algorithm.