Random Quotes Machine repeats

Hello, everyone! I need help with my quote machine. Sometimes my quotes, authors and colours repeat! How can i fixed this bag with Math.random? How can i exclude repeats?

const quotes = [ 
{quote: `"I'm not a great programmer; I'm just a good programmer with great habits."`,
author: `- Kent Beck`
}, {
quote: `"Talk is cheap. Show me the code."`,
author: `- Linus Torvalds`
}, {
quote: `"Programs must be written for people to read, and only incidentally for machines to execute."`,
author: `- Harold Abelson`
}, {
quote: `"Truth can only be found in one place: the code."`,
author: `- Robert C`
}, {
quote: `"Give a man a program, frustrate him for a day. Teach a man to program, frustrate him for a lifetime."`,
author: `- Muhammad Waseem`
}, {
quote: `"How you look at it is pretty much how you'll see it"`,
author: `- Steve Jobs`
}, {
quote: `"The most disastrous thing that you can ever learn is your first programming language."`,
author: `- Alan Kay`
}, {
quote: `"The most important property of a program is whether it accomplishes the intention of its user."`,
author: `- C.A.R. Hoare`
}, {
quote: `"I am committed to push my branch to the master."`,
author: `- Halgurd Hussein`
}, {
quote: `"Coding is not just code, that is a live thing to serve everyone!"`,
author: `- Ming Song`}
const colors = ['#FFC0CB]', '#DDA0DD', '#483D8B', '#F08080', '#EEE8AA', '#3CB371'];
let quoteText = document.getElementById('text');
let quoteAuthor = document.getElementById('author');
let backGround = document.getElementById('body');
function getRandom () {
    let randomQuote = Math.floor(Math.random() * quotes.length);
    let randomColor = Math.floor(Math.random() * colors.length);
    quoteText.innerHTML = quotes[randomQuote].quote
    quoteAuthor.innerHTML = quotes[randomQuote].author
    backGround.style.background = colors[randomColor];

You could create a helper array that holds the values that were already used.

const colors = ['blue', ..., 'red'];
const usedColors = [];

Each time you get a random color, you move it from colors to usedColors until you have no colors left in the colors array. Then you swap the arrays and start over.

There is probably a better solution but that’s the one I can think of now :wink:

1 Like

You can use a compare function that checks the previouse value to the curent one
If the value is already is used u can delete it

1 Like

Can you explain more about it? Should i use a loop for this? Sorry for such questions, i’m beginner(

How can i check previouse value?