samedi 27 mars 2021

random array of objects with no repeat

I use splice to show quotes with no repeat but I think I miss something cause q.length is'not change

this is my code:


async function JsonFile(){

    const response = await fetch('/quotes.json');
    const data = await response.json();

    const quotes = data.quotes; 
    getQuotes(quotes)
}

let backupArray =[];

function getQuotes(q){

    const random = Math.floor(Math.random()* q.length);

    name.textContent = q[random].name;
    quote.textContent = q[random].quote;
   const spliceEl = q.splice(random,1);
   console.log(spliceEl);
   backupArray.push(spliceEl);
   console.log(q.length);
   console.log(backupArray)
  

}

button.addEventListener('click' ,JsonFile)





Aucun commentaire:

Enregistrer un commentaire