Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Aaron Lipinski
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Aaron Lipinski
Full Stack JavaScript Techdegree Graduate 15,990 Points

Help getting Math.random() to not return the same value twice.

I currently have my function pulling a random quote from my array of quotes. I'd like to not repeat the quote when pulled. Im having trouble figuring out how to make this happen.

can achieve this which the current function I have made. Do I need to push quotes into a empty array then check with an if statement to compare is the quote already exists?

function getRandomQuote(){
    randomNumber = Math.floor(Math.random() * quotes.length);
        return quotes[randomNumber];
}

This function is called later in another function.

   function printQuote(){
    randomQuote = getRandomQuote(quotes);
    htmlToPage = '<p class="quote">' + randomQuote.quote +'</p>';
    htmlToPage +='<p class="source">' + randomQuote.source;
    if(randomQuote.citation){
        htmlToPage += '<span class="citation">' + randomQuote.citation + '</span>';
    }
    if(randomQuote.year){ 
        htmlToPage += '<span class="year">' + randomQuote.year + '</span>';
    }
    if(randomQuote.category){
        htmlToPage += '<span class="category">' +  " " + randomQuote.catagory + '</p>';
    }
    htmlToPage += '</p>';
    random_bg_color();
    document.getElementById('quote-box').innerHTML = htmlToPage; 
}

Hope this makes sense.

2 Answers

Steven Parker
Steven Parker
230,274 Points

An even better strategy might be to remove each quote as it is is used from the original array and store it in another one. Then, before picking one, check of the source array is depleted and restore it from the storage array if so. That guarantees that no quote will be shown twice until all have been used once.

If you care to search the previous questions, I'm pretty sure I've seen an example of this posted before (perhaps even by me!).

Aaron Lipinski
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Aaron Lipinski
Full Stack JavaScript Techdegree Graduate 15,990 Points

After searching and tinkering I think I got it. Although full transparency, I did have to look at some example code to get it.

function getRandomQuote(){
    pullQuote = Math.floor(Math.random() * quotes.length);
    spliceQuote = quotes.splice(pullQuote, 1)[0];
    usedQuote.push(spliceQuote);
    if(quotes.length == 0){
        quotes = usedQuote;
        usedQuote = [];
    }
    return spliceQuote;
}