Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript

vikas pal
vikas pal
11,207 Points

Please Help me to get this website work!

I have created a project on my own in which I want to let the user, to use the calculator on the website provided. I was able to make the keypad structure of the calculator, but now stuck in a problem.

That is how I will calculate the number? Please see the code from the below treehouse workspace fork and help me to calculate the result. I want you to capture the digits of the user by clicking on the digits provided in the keypad instead of typing.

You can also help me by making the CSS better.

The calculator will open when you click on the calculator field.

Here's the Treehouse Fork of this project:-- https://w.trhou.se/2w8ftzlphm

1 Answer

Steven Parker
Steven Parker
216,135 Points

:bell: Hi, I got alerted by your tag.

You seem to have a good start, but there's a bit more coding to be done.

To capture the digits, as you create the buttons, you could call "addEventListener" on each one to give it a function to handle the "click" event. Or, you could have a single delegated handler on the "space". Then, in that handler function you can store the digits and perform the calculations using the button label to choose the action. You still need to add an element to act as the "display" of the calculator where you can show the result.

And one suggestion for the CSS would be to add "cursor: pointer;" to the buttons to visually indicate that they are clickable.

vikas pal
vikas pal
11,207 Points

But how I store it in a variable. Say if a user click 7 and a 2 button to type 72 then how I am able to store it. How I store it in the ten's form.

Steven Parker
Steven Parker
216,135 Points

You have options, but one example would be if you had a global variable named "input", that started out as an empty string. Then, when one of the number buttons was clicked you could do something like this in the event handler:

  input += e.target.textContent;

Then when one of the operator buttons was clicked, you might do this to convert all the collected digits into a number and save that in another global named "operand":

  operand = parseFloat(input);
vikas pal
vikas pal
11,207 Points

But what when the number is a float? means we cannot use parseInt() on the input.

Steven Parker
Steven Parker
216,135 Points

To support floating point numbers you could use "parseFloat" instead. I've changed it in my previous suggestion.

vikas pal
vikas pal
11,207 Points

Yeah, but how I will know that what the number is? an integer or float to use the respective function.

Steven Parker
Steven Parker
216,135 Points

The "parseFloat" function will convert any number. You will not need a different function for integers.

vikas pal
vikas pal
11,207 Points

Oh! yeah, you're right. Thnks for your great help:-)