0

Problem while removing a event listener in a tic-tac-toe game in JS

https://www.sololearn.com/compiler-playground/WjZvaM7DhRBg. When I want the game to end, and remove the event listener, it doesn't work. Please help.

5th Apr 2024, 4:40 PM
Charbel
5 ответов
+ 1
Yeah pictures didn't show it made it confusing. So I followed the links to your GitLab repository, forked the project, and imported it into VSCode. I observed that your code functions, but it does not account for every scenario. It only checks for this: // verify lines l1; l2; l3 It's necessary to consider all other possibilities to ensure the condition can evaluate to true. I added some placeholders if anyone else wants to help. https://sololearn.com/compiler-playground/WpM7vuCG8lw0/?ref=app
6th Apr 2024, 2:41 AM
Chris Coder
Chris Coder - avatar
+ 1
And also your JavaScript in this platform (soloLearn) should be inside the window onload function window.onload = ()=>{ //here }
6th Apr 2024, 6:51 AM
IAmSupreme
IAmSupreme - avatar
0
TBH, I haven't taken time to see if and why your removeEventListener isn't working as result page didn't render anything and your code doesn't not look properly indented .... But you can try this shorter alternative: var disabled = false; element.addEventListener("click",()=>{ if(!disabled){ //Perform actions } }); In this case the «disabled» variable determines if the action should be performed or not and can be altered in an external function
5th Apr 2024, 11:00 PM
IAmSupreme
IAmSupreme - avatar
0
the code above was meant to be an example, taking to consideration that you'll be needing 9 disabled variable for 9 tictactoe buttons, I suggest you create an array for it: var button = document.querySelector(".button"), /*gets all elements with class button, you can change to your convenience*/ disabled = new Array(button.length); /* an array with with each index representing the element with that index in button */ for (let i=0; I<button.length; i++){ disabled[i] = false; //initialize button[i].addEventListener("click",()=>{ if(!disabled[i]){ // Perform action for button } /* If disabled with index i is false, button[i] will perform action else otherwise */ } }
6th Apr 2024, 6:48 AM
IAmSupreme
IAmSupreme - avatar
0
Thanks a lot
6th Apr 2024, 11:03 AM
Charbel