Console value is unexpected | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Console value is unexpected

The if-else switch isn't working as it expects to call p1 -> p2... https://code.sololearn.com/WW3BM46nLu9t/#js

20th Jul 2020, 2:20 AM
zexu knub
zexu knub - avatar
4 Answers
+ 5
Elements can have multiple event listeners for the same event. addEventListener does not remove the previous functions(callbacks) that are waiting for the event triggering. When you "play()" you are adding a new function(and another,and another...) to be called when the user clicks. A way to solve this would be to call removeEventListener before adding a new one but that's ugly. Simply avoid calling addEventListener within your play function. I used a global variable to keep track of the current player and a loop to avoid code repetition: https://code.sololearn.com/W8eFWdcY2eVS/?ref=app Tip: Don't change your code while the question is unanswered. That makes things harder for your helpers๐Ÿ˜….
20th Jul 2020, 3:54 AM
Kevin โ˜…
+ 6
Hey, change that face. It's ok, this things have happened a lot to me too and to everyone. Keep practicing, you are doing a good job already and I'm sure you will be able to improve it. Feel free to keep inquiring if you have more doubts.
20th Jul 2020, 4:42 AM
Kevin โ˜…
+ 1
Kevin โ˜… Thank You ;)
20th Jul 2020, 4:46 AM
zexu knub
zexu knub - avatar
0
Kevin โ˜… i was trying to make it work somehow but thank you so much it makes sense now. And also thank you for clearing the repetition. ๐Ÿ˜“
20th Jul 2020, 4:25 AM
zexu knub
zexu knub - avatar