Bug in calculator project

hey, everyone. I’m working on the calculator project, and I was hoping that someone could give me a hand figuring out a bug in my javascript. Currently it can clear all, clear last entered number, and “add”, but the add is kind of a cheat and thats where my problem is. Somewhere between hitting equals and running the calculation there is something going on. I think it has something to do with my reference to the plus operator, and maybe there is a way that it could be written better. I made a bunch of notes… any help would be appreciated.

There are many problems. Too many to mention. It is one of the hardest challenges as there are so many cases to deal with. The one I noticed is you allow pressing of multiple operators together.

I’m working through it piece by piece. Just as I worked out clear and clear all one at a time, right now I just want it to be able to add two numbers and output the correct value while showing everything that was already entered. Too many problems to mention really isn’t helpful. You may as well just said you suck and I don’t think you deserve my help. If you see a problem with what I have it would be more helpful to me to get constructive criticism then to throw a blanket, you have tons of mistakes. Why say anything at all? Is this not supposed to be a help forum???

I gave you an example. I spent at least 4 weeks working on this problem. To be honest I did not understand:

Currently it can clear all, clear last entered number, and “add”, but the add is kind of a cheat and thats where my problem is.

What do you mean “cheat”?

Also

maybe there is a way that it could be written better.

is such a vague statement. Almost every piece of code can be “written better”.

The add is a cheat because the condition of the if statement will always be true if the condition is written as = ‘+’ because it’s just assigning a value rather than checking for an instance of ‘+’. If it is written as == ‘+’ then it evaluates to false and then just prints the initial value of result, which is 0. In my notes on the JS file I wrote a comment asking how I could possibly reference the ‘+’ or maybe there is a better/different way to check if the operator is/was a + - / or *. I like to break my code down into bite size pieces and work through each problem one by one, so working on making sure it can do the simplest things first and then implementing code later that will check for more in-depth conditions. I appreciate that you thought of a condition to check for later(multiple operator clickability), but that is way beyond the scope of my current problem. I thought the comments on my project page were clear in stating that I was looking for a reference to plus or a better idea of how to implement logic that says, hey the plus button was clicked so add the numbers in the array. I guess I need to change them so it’s more clear that I’m only looking to add right now. I do appreciate the help, and like I said, I am open to critique, but it just seemed as though you didn’t read what was being asked. Apparently, I wasn’t clear enough, so I do apologize for not stating earlier that all I need is help understanding how to reference ‘+’ or possibly some direction to a better solution. I’m currently thinking that maybe setting up a click check that returns true to the calculate function might be my best bet, but that seems like it might only allow for two number operation e.g 10 + 10 = 20 instead of being able to do 20 + 30 + 40 - 5 = 85.

i press 3+3 =
line 87 …stored.operands … 3,3

line 73 if(stored.formula.textContent == ‘+’) this is always false as it is 3+3 or if you pressed 5+5 then it would be 5+5
if you change to if(stored.formula.textContent == ‘5+5’) and press 5+5= on calc then it goes true (but you cant have it checking for 5+5 lol) and enters the if … where it hits another problem
for(var i = 0; i < length; i++) { should be for(var i = 0; i <arr.length; i++) {
then it would return 10
use regular expression will sort out prob as i checked and it works
change line 73 to if(/[+]/.test(stored.formula.textContent) … this checks to see if stored.formula.textContent contains a ‘+’

how are you on debugging as you can see all this if you step through code in chrome debugger … if you are not used to debugging … you should really work on learning how to debug it will really really make your life easier

2 Likes

The way I did the maths was to use the eval function. One for you to investigate.

2 Likes

I would have thought that because it’s not strict equals === that it would have passed just as 5 == ‘5’ //=> true and 5 === ‘5’ //=> false. Obviously, this is not the case or wouldn’t be here posting a question. :slight_smile: And no I’d have a pretty useless calculator if it only checked for 5 + 5…LOL

Why do you say that I should use arr.length in the for loop? It has been my understanding that it is better to create a variable that gets the length because it’s “less expensive” to use the set value of the variable length over checking the length through each iteration. Can you explain why arr.length is better?

I haven’t learned about regular expression yet, but I’ve seen it. That’s awesome, though. Simple, concise. I just finished up a thing on object literals - that’s part of why I chose to use them for my project. Outside of the obvious issue I’m currently having, and negating that I have a ways to go before this app is complete, what do you think of my code, otherwise? Any suggestions as I move forward with learning and, as well, building my app?

The only thing I have for any sort of “debugging” is js lint in my text editor, brackets. I have never used a debugger, but thank you for the advice, I will be sure to look into that this weekend.

I think I’m going to have to hit the MDN and look over regular expressions so I can understand what you have written and not just copy and paste.
I appreciate you taking the time to look over my project. I look forward to hearing back…

I will look into that as well as John’s result. Thank you for the information.

1 Like

that it would have passed just as 5 == ‘5’ //=> true and 5 === ‘5’ //=> false
example… var num = ‘5’ and var x = 5 … num == x would be true but num === x would be false
but the problem is your comparing stored.formula.textContent == ‘+’ and this == or === is only used to compare text numbers with numbers … if stored.formula.textContent was ‘+’ it would always result in true by using == or ===

if you press 3 on your calc then press + then press 5 then press = … stored.formula.textContent would equal ‘3+5’ and your if would be if(‘3+5’ == ‘+’)
thats why i changed it to a regular expression /+/ … this check to see if the string contains a + by using the test method eg /+/.test(string) and as ‘3+5’ contains a + it evaluates to true.

next why did i say to use arr.length because if you look at your for loop you have for(var i = 0; i < length; i++)…??? length of what ??? i thought you just forgot the arr … but if you dont want to use arr.length you need to create a variable before the for loop eg var len = arr.length. then use
for(var i = 0; i < len; i++)

learn to use regular expressions … use links below … its difficult starting off … but its amazing what you can do with it.
https://regex101.com/

you have access to chorme dev tools to debug with if you use chrome … google how
or if you use firefox google how to debug uing firefox … it is joint number one … most important thing to be able to do if you are serious about coding
my other most important number one … is working out the logic of a project … how it works and writing it in Pseudocode code. Debugging is pretty easy to pick up just the more you do it the easier it gets and the more you get from it. you can also use this site pythontutor.com … you can write your code in it or just paste it into it (set it for javascript ES6) and then you can step through your code line by line … really helps but dosent show as much as say the chrome debugger dose so aim to learn how to debug in chorome.
anyway hope this helps

1 Like

That makes sense.

As far as the arr.length in the loop goes, I do have a variable set up before it that gets the length. I was just wondering if it was better/faster/whatever to use i < arr.length vs the use of var length = arr.length; and then i < length in the loop condition. I will definitely look into regular expressions and thank you for the links. I am extremely serious about learning to code so I will also look into using the debugger. I’m betting everything I have on making this my living. I started about 3 months ago not knowing anything and started about 3 1/2 weeks ago on javascript and I love it.
thank you again for all of the advice, I’m sure it will pan out as being very helpful.