Help with calculator - performing the arithmetic

Help with calculator - performing the arithmetic
0

#1

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???


#2

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

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


#4

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


#5

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


#6

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:


#7

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


#8

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…


#9

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


#10

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


#11

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


#12

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


#13

Here’s my calculator.

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


#14

@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.


#15

What is ‘shunting yard algorithm’ ?


#16

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.