Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Kyle Jensen
PLUS
Kyle Jensen
Courses Plus Student 6,293 Points

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 that's 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. Also, if you change the = to == in the calcuate() then the formula and operator output is correct but the answer then becomes 0. I made a bunch of notes.. any help would be appreciated. http://codepen.io/asthewater_falls/pen/Rpoayq?editors=0010

1 Answer

Steven Parker
Steven Parker
230,274 Points

Substituting an assignment for a conditional test is always a bad sign.

Clearly, the conditional test is not doing what you are expecting it to do. The test fails (when it is a test, using "==") in your example because stored.formula.textContent is "5+5" when you compare it to "+".

I'm not sure what you may have intended, but perhaps you might want to store the previous (or last non-equal) operator in a new property of stored, and use that for the test instead of the entire formula. Otherwise, you might want to add some code to extract it from the formula.

Also, I had to re-position the total in the CSS to be able to see it. I don't think that flexbox and absolute positioning work well together.

Kyle Jensen
Kyle Jensen
Courses Plus Student 6,293 Points

Steven Parker My intention was to be able to show everything entered by the user. And, yes, the conditional has been a thorn in my side. That is a good suggestion. I will give it a try. HMM.. I don't think there should be a position absolute on the total, just set to flex-end, but I will look at it. The only one that should have absolute would be the operator, but I have a solution to resolve that and keep it just flex-box. I will take a look at those things in addition to your advice on the JS. I really appreciate your input. Just out of curiosity, what browser do you use?

Kyle Jensen
Kyle Jensen
Courses Plus Student 6,293 Points

Steven Parker I see what you're talking about with the total now. When you change the code, is the a permanent change? Because I wasn't having that problem before and I wonder if someone else messed with it.

Steven Parker
Steven Parker
230,274 Points

I use Chrome for all Treehouse-related things, since I believe that's what Treehouse uses to develop them.

I have not used codepen for public sharing myself, but I would expect that guest viewers cannot modify your code unless you have explicitly set that up in the configuration. I tested changing a few things just now and they went back when I re-opened the page.

Kyle Jensen
Kyle Jensen
Courses Plus Student 6,293 Points

Steven Parker Ok, thank you. I made a couple of adjustments to the CSS that seem good on my end. Could I trouble you to go back and see if you have any trouble with viewing the total now? If there's no bug there then I can move onto fixing the JS.

Steven Parker
Steven Parker
230,274 Points

I notice you haven't selected a "best answer" — is your original question still unresolved?