jeudi 8 avril 2021

Array push does not do his job

Why does container.push(random[0]) not work?

I can't find a solution to the problem... I need to put the HTML Object into the container array, and well it does not go there.

let container = new Array();
let x = document.querySelectorAll('.trigger');

function getFlag() {
  let random = [x[Math.floor(Math.random() * x.length)]];

  if (!container.includes(random[0])) {
    random[0].classList.remove('trigger');

    console.log('random n: ' + random[0]);
    container.push(random[0]);
  }
}

console.log('x length: ' + x.length)
console.log('array length: ' + container.length)
.trigger {
  display: none;
}
<body onload="getFlag()">
  <section class="trigger">
    <p>1</p>
  </section>
  <section class="trigger">
    <p>2</p>
  </section>
  <section class="trigger">
    <p>3</p>
  </section>
</body>



Aucun commentaire:

Enregistrer un commentaire