Home > Software design >  I want the array to run again from the start once it reaches the end
I want the array to run again from the start once it reaches the end

Time:09-05

This is my code:

const quotes = ['"Current Quote"',]
  let i = 0;
   
  shuffle.addEventListener('click', () => {
    i = i 1
    nxtQuote = quotes[i]
    quote.textContent = nxtQuote
  })
}

CodePudding user response:

you need to set it to 0 when it reaches the end and there were imbalanced brackets

var shuffle = document.getElementById("shuffle");
var quote = document.getElementById("quote");

const quotes = ['"First quote"', '"Second Quote"', '"Third Quote"'];
let i = 0;
quote.textContent = quotes[i];

shuffle.addEventListener('click', () => {
  i  ;
  if (i == quotes.length) i = 0;
  quote.textContent = quotes[i];
});
<button id="shuffle">Shuffle</button>
<p id="quote"></p>

  • Related