Getting elements from one function in another one

  arrays, function, javascript

This functions work fine when they work alone, but when I try toggle between them, I got same elements in both arrays. Some are multiply by numbers of clicks others not.

var playerBtn1 = [];
var playerBtn2 = [];

var btn = document.getElementsByTagName("button");

playerA();

  function playerA() {
    
    for(let i = 0; i <btn.length; i++) {
        btn[i].addEventListener("click", () => {
            playerBtn1.push(btn[i]);
            playerB();
            
        }); 

        
}

return playerBtn1;
}
    

function playerB() {

    for(let k = 0; k <btn.length; k++) {
        btn[k].addEventListener("click", () => {
              playerBtn2.push(btn[k]);
              playerA();       
    });  
    
    }

    return playerBtn2;
    
}  

Source: Ask Javascript Questions

LEAVE A COMMENT