Home > Enterprise >  How to push numbers into an array every time a function runs a la [1, 2, 3, 4, 5, 6 etc...]?
How to push numbers into an array every time a function runs a la [1, 2, 3, 4, 5, 6 etc...]?

Time:09-21

const p1Wins = [];

function raiseScore() {
  if(xWinMessage) {
    const p1Name = document.querySelector('.p1-name');
    p1Name.style.color = 'green';

    for(i = 0; i < p1Wins.length; i  ) {
      p1Wins.push(i)
    }
    
    let p1Score = document.querySelector('.p1-score');
    p1Score = p1Wins
  }
}

Not sure what I'd need to do to make this work. If anyone could help that'd be greatly appreciated.

CodePudding user response:

If you just need to add [1,2,3,etc] in that order. You could do somthing like this.

 const p1Wins = [];
 
 const pushStep = () => p1Wins.push(p1Wins.length 1);
 
 // call in the function
 pushStep();
 pushStep();
 pushStep();
 
 console.log(p1Wins);
 
 

  • Related