# Simple JS Calculator - I need some help please!

Hi there!
I am new with JS and testing a super simple calculator with only 2 variables that add Apple and Cherries.
Can someone teach me how to calculate the total of apples and Cherries added and display the total number?

Here is the Codepen

Thank you
P.

I would expect that you want the total to update on every click, right?

So, I would have a function like âupdateTotalâ that gets the values for each and then puts that total, just like you update the value for cherries with `countCherries.innerText = count`. I would call that new function at the end of addApple and addCherries.

2 Likes

Your two buttons has the same ID, I would change that also.

UPDATE. Aslo your logic is not quite right I think - you have one variable count for both apples and cherries.

Try to click both buttons in a random order - you will see why it is a problem

2 Likes

I want to update the total on every click âTotal resultâ which is the Total of Apple and Cherries.

Like this?

May I ask you to share the line of code? I am super new with JS and do not understanding how to code;(

May I ask you to show the write syntax? i just want to learn as quick as possible.

I mean, itâs a common rule - id attribute must be unique. As long as you donât have two elements on the page with the same id - it is fine.

I just refactored your code a little, but it is not best practice just show the direct solutions here.

If you will solve this yourself you will learn bunch of stuff, this is actually nice exercise for DOM beginners

1 Like

Iâd rather guide you to the answer. People donât tend to learn from cutting and pasting. And this is a learning platform.

You have this for cherries:

``````let countCherries = document.getElementById("count-cherries")

count = count + 1
countCherries.innerText = count
}
``````

Iâm saying to do something similar for âtotalâ. First you create a reference to that spot in the DOM - you called it âcountCherriesâ there, but maybe now it would be âtotalâ. Then you have a function - instead of âaddCherriesâ, youâll have âupdateTotalâ.

Inside updateTotal, you get the values for cherries and store it in a variable. You have âcountCherries.innerTextâ that can access that value, but remember that this is a string so youâll have to convert it to a number. Youâll do the same for apples. Then youâll add them together and put them in the âinnerTextâ of your total.

1 Like

I also think that you want separate counts for apples and cherries. Right now they are both the same variable. Really, I donât think you need the variable count - you already have the numbers stored on the DOM - why store it in two places. You could use `countCherries.innerText` to access that number directly and store it in a variable local to that function, instead of keeping a global count.

I went ahead and gathered bunch of articles about methods which may be used in this exercise(well, at least it is what I used):

You need to dive into this stuff sometimes, you need it even if you want âlearn as quickly as possibleâ. Just asking people for showing right syntax will not help you to get better.

Also I will leave the link at DOM course from FCC youtube, which I used to get started with DOM:
here it is

3 Likes

Thank you! I agreeâŚ let me try and dive into thisâŚ

Yeah, there is. This is hard stuff, thatâs why it pays well. But if you stick with it, youâll get there.

Try what weâre suggesting and check back if itâs not working.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.