# Random Quotes Machine repeats

**URL:** <https://forum.freecodecamp.org/t/random-quotes-machine-repeats/415938>\
**Category:** I Built This\
**Created:** [August 18, 2020, 11:03am UTC](https://forum.freecodecamp.org/t/random-quotes-machine-repeats/415938 "2020-08-18T11:03:50Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![matveyka.boltach](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/matveyka.boltach/32/134249_2.png) [@matveyka.boltach](https://forum.freecodecamp.org/u/matveyka.boltach)\
**Post date:** [August 18, 2020, 11:03am UTC](https://forum.freecodecamp.org/t/random-quotes-machine-repeats/415938/1 "2020-08-18T11:03:50Z")

</div>

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?

```auto
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];
    
}

```

---

<div class="post-metadata">

**Author:** ![sitek94](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/sitek94/32/336491_2.png) [@sitek94](https://forum.freecodecamp.org/u/sitek94)\
**Post date:** [August 18, 2020, 11:13am UTC](https://forum.freecodecamp.org/t/random-quotes-machine-repeats/415938/2 "2020-08-18T11:13:02Z")

</div>

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

```auto
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 😉

---

<div class="post-metadata">

**Author:** ![KittyKora](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/kittykora/32/124629_2.png) [@KittyKora](https://forum.freecodecamp.org/u/KittyKora)\
**Post date:** [August 18, 2020, 11:14am UTC](https://forum.freecodecamp.org/t/random-quotes-machine-repeats/415938/3 "2020-08-18T11:14:13Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![matveyka.boltach](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/matveyka.boltach/32/134249_2.png) [@matveyka.boltach](https://forum.freecodecamp.org/u/matveyka.boltach)\
**Post date:** [August 18, 2020, 11:18am UTC](https://forum.freecodecamp.org/t/random-quotes-machine-repeats/415938/4 "2020-08-18T11:18:33Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![matveyka.boltach](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/matveyka.boltach/32/134249_2.png) [@matveyka.boltach](https://forum.freecodecamp.org/u/matveyka.boltach)\
**Post date:** [August 18, 2020, 11:23am UTC](https://forum.freecodecamp.org/t/random-quotes-machine-repeats/415938/5 "2020-08-18T11:23:52Z")

</div>

How can i check previouse value?

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [June 1, 2021, 1:12pm UTC](https://forum.freecodecamp.org/t/random-quotes-machine-repeats/415938/6 "2021-06-01T13:12:34Z")

</div>

Closed on June 1, 2021, 1:12pm UTC


