I’m extremely new to coding and I probably did a lot of things the wrong way, but could you guys look at the calculator and tell me how to make the + function right. Nothing else, I want to find the other things out on my own. The Calculator
As it says in the Calculator Project, there are two different schools of math involved: immediate execution logic or expression logic. Which you implement will determine how you handle this.
At this point, you seem to be simply building a string, and then it appears you wish to parse that string to determine the operations? If that’s the case, then you have some logic to consider before you render operations ( + - * / ). First, how will you divide the string into a series of steps? Second, how will you execute those steps?
Do you have some functional logic backing your design? I’m not being a jerk, simply trying to determine what you see as your order of steps.
Allright, sorry to bother with my stupidity thinking I could do this. I’ll start over and follow the steps of that Calculator Project. Thanks!
I think you CAN do this, but it sounds like you may have decided to jump right in at the deep end. Am I wrong in assuming that?
I truly don’t think it’s a stupidity thing, I think its a willingness to jump in and try. Nothing wrong with that. But my point was (and is) spend a little time on design, and it will pay off when you get to development.
To answer your question directly, I created a single event listener attached to all the buttons with an
operator class. Then I created a switch statement, handling each case ("+", “-”, “*” and “/” – “=” is a special case I handle elsewhere). Depending on the operator, the switch will perform the appropriate math. I used immediate execution logic, much easier to simply calculate left to right.
But in essence, if the switch statement sees that the clicked button has an innerText value of
+, then it takes a running total and adds the next number to it. Same logic for each operator.
Okay thanks, I’ll try this and see if I can get it to work.
Have not yet looked at your code, but if you build a string expression you can just use eval() function. not a very safe function in some cases but if you are in control on whats going into that string, i think its no problem in using it.
Allright thanks, I also got some tips from snowmonkey I’m going to look into.