As usual with these things, there are elements of this project that are not going to be trivially easy for you, but if you’ve been following the course so far, you definitely have everything you need to finish it. We’re going to walk you through the various steps you can take, but again, how you actually implement them is up to you!
eval() function. However, this function can be very dangerous and should not ever be used! You’ll need to build your own functions to evaluate expressions as part of this calculator project. On the same note, when researching how to calculate expressions for this project, you may encounter solutions that suggest that you return a
new Function() that evaluates a string. Similarly to
eval(), this should not be used due to potential pitfalls of evaluating insecure data. Besides, where’s the fun in solutions that do all the work for you? Let’s get to it!
Don’t forget to commit early & often! You can reference the Commit Message lesson here!
Here are some use cases (abilities your project needs to have):
operatethat takes an operator and 2 numbers and then calls one of the above functions on the numbers.
operate()on them when the user presses the “=” key.
operate()has been called, update the display with the ‘solution’ to the operation.
12 + 7 - 5 * 3 =should yield
42. An example of the behavior we’re looking for would be this student solution.
12), followed by an operator button (
+), a second number button (
7), and finally a second operator button (
-). Your calculator should then do the following: first, evaluate the first pair of numbers (
12 + 7), second, display the result of that calculation (
19), and finally, use that result (
19) as the first number in your new calculation, along with the next operator (
=before entering all of the numbers or an operator could cause problems!
.button and let users input decimals! Make sure you don’t let them type more than one though:
184.108.40.206. It is hard to do math on these numbers. (disable the decimal button if there’s already one in the display)