+ 3

Can someone plz help me with my code?

I tried running many diff addEventListeners on diff buttons but they interfere with each other. In my code, if u add a two players and add 5 for the previous player, it adds 10 instead. And if it three players it adds 15 for the first plaayer added. What is the best way to add a player and run an click function that wouldn't affect other buttons. The forEach loop is making it affect other butons. https://code.sololearn.com/WN07Y55jabA5/?ref=app

28th Jul 2020, 8:22 AM
Fasasi Sherif
Fasasi Sherif - avatar
4 odpowiedzi
+ 2
I found it very difficult to follow your code so I heavily refactored it and rewrote a lot here: https://code.sololearn.com/W64ygmDRq7K1 The bugs should be fixed with my version but I wasn't able to find precisely where your problem originally was. I was overwhelmed by the indentation problems, weak naming of variables and functions like theFunc, myFunc... and was unable to follow what a lot of it was trying to do. My approach was like finding a needle in a hay stack by reorganizing every straw in the haystack. It is just hard to know where the needle was in the original state of the haystack. A few things I changed are: - moved all JavaScript from the HTML tab to the JS tab. JS tab is a better place for it. - removed the single player that was templated into HTML so JavaScript had full control over each player's document structure. In other words, no more cloning. Reason: HTML can be nicer to lay out the document but the mixture of cloning and having JavaScript still assume lots of the structure to fill in score, name, date... is more complicated. - refactored a lot of JavaScript to work around some clearly named functions. - Made it so an Array called playerData represented all player data and functions like renderPlayerList would regenerate the entire document model for the list whenever any data in the playerData was updated. Manipulating the playerData variable and regenerating the document looked easier to read, understand, troubleshoot, and maintain than manipulating the document in special ways each time a little change was needed. - a minor change was that I replaced ampersand with the corresponding HTML entities in the link tag because Sololearn's code editor was indicating that they were invalid.
28th Jul 2020, 3:11 PM
Josh Greig
Josh Greig - avatar
+ 2
Thank you so much for your help. I really appreciate it. I'm going through ur code and I'll let u know, if I have a problem.
28th Jul 2020, 3:56 PM
Fasasi Sherif
Fasasi Sherif - avatar
+ 2
I had the notion that I the cloning was causing the problem. But it was difficult to start again. Thank you very much once again for your elaborate explanation. I've gained so much from it.
28th Jul 2020, 3:58 PM
Fasasi Sherif
Fasasi Sherif - avatar
+ 2
I have corrected my code now. After understanding ur code, I did mine. Thanks very much.
30th Jul 2020, 10:25 PM
Fasasi Sherif
Fasasi Sherif - avatar