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

jason limmm
jason limmm
8,009 Points

problem with calculator

here is my snapshot:https://w.trhou.se/h3djzh8ot5

i am having trouble with the calculation part of my js code, i'm just lost and don't know what to write

here is my code:

// calculate
equals.addEventListener("click",()=>numbers.innerHTML = {

});

i've made so i can put numbers inside a number input box via clicking on the number basically like a traditional calculator where you click the number, but i just don't know how to make the program calculate and display the answer.

it's a lot to ask i know but i also want to learn how the process works. sorry if this sounds confusing i don't really know how explain it.

1 Answer

Steven Parker
Steven Parker
231,198 Points

I seem to recall there's an exercise much like this in one of the courses … but you haven't referenced a course so maybe this is unrelated to Treehouse. Is this perhaps a homework assignment?   :see_no_evil:

There are several different approaches for doing this. When the equal sign is pushed, you could parse the entire formula string and perform the calculation. But this would be rather complicated.

An easier method might be to convert the string of digits in the display ("numbers") into an actual numeric value whenever an operator button is pushed. Then store that number and the operator in variables while the 2nd set of digits is entered. After that, when the equals button is pushed you would again convert the string into a number, but then perform the stored operation using the new number and the previously stored one. The result can then be displayed in the box.

Also, the snapshot did not function initially due to a spelling issue in index.html:

         <script src="calcution.js"></script>    <!-- needs to be "calculation.js" -->
jason limmm
jason limmm
8,009 Points

oh yeah, i am also using html entities and i don't how to convert them into +, -, / and * respectively

Steven Parker
Steven Parker
231,198 Points

I'm not sure why you'd need to convert anything. Each button can have a separate handler that stores whatever you want.

jason limmm
jason limmm
8,009 Points

i don't really know how to convert the process you gave into actual code, i need some help please😅 sorry for the inconvenience

Steven Parker
Steven Parker
231,198 Points

I assume you're here to learn how to do these things, so perhaps you might set this project aside for the moment and take a few more courses.

I looked for that course I remember taking that had a calculator project in it, but it turns out it was retired 6 years ago and the replacement uses different example projects. It was the older version of Object-Oriented JavaScript, and the Support folks might be able to give you access to it on request (but don't be surprised if they can't).

I did however find an old forum post with links (that still seem to work) to calculators that students built on public code sharing platforms. That post is "Please give critic on my calculator that I've built :-)". But be aware that looking at their code might be helpful or confusing, depending on how different their approach was.

You could also ask an AI like ChatGPT to write an app for you. But if this is really for a homework assignment, a savvy teacher might spot the AI-generated code if you try to use it verbatim instead of just for inspiration! Also, I tested it and sometimes it used a peculiar approach that relied on the highly discouraged "eval()" function.

Steven Parker
Steven Parker
231,198 Points

Were you able to work it out and/or were you able to get access to the older course?

jason limmm
jason limmm
8,009 Points

no, i haven gotten the older version yet and i still can't figure it out, i did as you as said to learn more courses first before i move on and code but i don't know what course to learn so i just chose the full stack js track for now

Steven Parker
Steven Parker
231,198 Points

That will no doubt be of value to you in general. As to this task, did you try having the AI do it? But if it uses "eval()" tell it to "do it again but without using eval()".

jason limmm
jason limmm
8,009 Points

no, i'm afraid the code that the ai gives me code is written in a way that i can't understand

Steven Parker
Steven Parker
231,198 Points

Continuing with the courses should help with that also. Happy learning!