#issue with addition in calculator
39 messages · Page 1 of 1 (latest)
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);```
what class are you taking? there have been like 10 other people asking these same calculator questions over the last couple of weeks.
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
- you shouldn't use
==or!= - you shouldn't use
innerHTML - you should always declare your variables
innerHTMLis never going to equal0(number not a string)?- you should use event delegation instead of calling
getNumber()10 times đą - stop adding event handlers in every single one of your functions. it happens multiple times because you keep adding event handlers over-and-over.
innerHTMl then i convert it to a number
(result.innerHTML == 0) &
the question mark in that item was because == probably handles it for you, but don't do this.
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
fix everything I listed and it will work. it's never going to work properly with those issues present.
its even harder to do rn
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.
how do i fix it so it still clicks on the button
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.
i have 1 listener for each buttomn
need more specific help
its too confusing i dont know how to change it
No you donât. You keep adding new listeners every time you call those functions.
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