#issue with addition in calculator

39 messages · Page 1 of 1 (latest)

sick tapir
#

need some help here is the console

#

when i do 10 + 11 + 12 = 33 (ALL GOOD!)
when i do 10 + 11 = 21 + 12 = 54 ?!?!

#
getNumber(one);
getNumber(two);
getNumber(three);
getNumber(four);
getNumber(five);
getNumber(six);
getNumber(seven);
getNumber(eight);
getNumber(nine);
getNumber(zero);
doAddition(equal, addition);```
ancient pebble
#

what class are you taking? there have been like 10 other people asking these same calculator questions over the last couple of weeks.

sick tapir
#

im in a bootcamp

#

@ancient pebble

#

im making my entirely own calculator

#

ive no idea about someone elses work but im legit stuck

#

o shit i missed a fuunction

#
function finalAddition(complete, finalFirst) {
    complete.addEventListener("click", function equals() {
      console.log("First half: ", finalFirst);
      secondHalf = result.innerHTML;
      finalSecond = Number(secondHalf);
      console.log("Second Half: ", finalSecond);
      End = finalFirst + finalSecond;
      console.log("1st: ", finalFirst, "+", "2nd: ", finalSecond, "=", End);
      result.innerHTML = End;
      finalFirst = End;
  });
}```
#

my issue mainly is why is finalAddition running twice?

#

its because i do + each time but i need it to only run once

#

the answers shows in my console but then it re-iterates again

ancient pebble
#
  1. you shouldn't use == or !=
  2. you shouldn't use innerHTML
  3. you should always declare your variables
  4. innerHTML is never going to equal 0 (number not a string)?
  5. you should use event delegation instead of calling getNumber() 10 times 😱
  6. stop adding event handlers in every single one of your functions. it happens multiple times because you keep adding event handlers over-and-over.
sick tapir
#

innerHTMl then i convert it to a number

ancient pebble
#
(result.innerHTML == 0) &

the question mark in that item was because == probably handles it for you, but don't do this.

sick tapir
#

hm

#

ok

#

do you know how i can fix the issue with 10 + 11 = 21 + 12 ?

#

it shows in console log it comes to 33

#

but then it runs again and becomes 54

#

i want it to stop at 33

ancient pebble
#

fix everything I listed and it will work. it's never going to work properly with those issues present.

sick tapir
#

its even harder to do rn

ancient pebble
#

Don’t make a second post for the same issue. Sorry you don’t want to fix what you were told to fix, but the multiple event listeners is the cause of your issue.

sick tapir
#

how do i fix it so it still clicks on the button

ancient pebble
#

Your code doesn’t “click on the button”


#

It is an event listener

#

You should add one listener to each button, not keep adding multiple listeners to the same buttons.

sick tapir
#

i have 1 listener for each buttomn

#

need more specific help

#

its too confusing i dont know how to change it

ancient pebble
#

every time you call finalAddition(), you're adding a new event listener to the element

#

you deleted the rest of your code, so nobody can point out exactly where you've gone wrong. like I said, fix allthe issues I pointed out and your code will work.

#

you need to stop treating addEventListener() like a function that is called when you call it... you're assigning a function to be called when the click event happens.

#

when user clicks, do X

#

so if you add that event listener multiple times, user clicks once and it fires multiple times