Below is a snippet of my code.. I'm working on a random quote generator and would like to find a solution on generating each random quote and background from the arrays without repeats until all have been used, and then cycling through again. The background images are being imported directly into the state and the quote array is imported from a separate file. I've attempted slicing each item from the array and pushing it into an empty one, but so far that has been unsuccessful.
constructor(props) {
super(props);
this.state = {
quote: quotes[0].quote,
author: quotes[0].author,
backgrounds: [
photos..
]
};
}
shuffleAll = (arr) => {
return arr.sort(function () { return 0.5 - Math.random() });
}
findRandomQuote = (arr) => {
let num = Math.floor(Math.random() * quotes.length);
let randQuote = quotes[num];
this.shuffleAll(quotes);
this.setState({
quote: randQuote.quote,
author: randQuote.author
});
}
findRandomBackground = (arr) => {
const { backgrounds } = this.state;
let background = backgrounds[Math.floor(Math.random() * backgrounds.length)];
this.shuffleAll(backgrounds);
document.body.style.background = `url(${background})`;
}
quotes? Please try creating a minimal reproducible example