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

how to make sure the same quote doesn't get return until all of the quotes have been display at least once in the array?

on Tech Degree 1, we was asked to return a random quote in the array, every time a user click on the button.

But...... what do you think i will need to do to make sure it doesn't return the same quote until all of the quotes have been displayed at least once?

Hint: you would add this logic to the getRandomQuote function, where you would find a way to print all the quotes randomly, one at a time, eliminating the quotes as you go, and then once all the quotes had been displayed, you would start over and repeat the process with the full list of quotes.

cant figure this out, been trying all day, please help

4 Answers

Steven Parker
Steven Parker
243,266 Points

There are a few ways to do this, but one approach would be to print a randomly-selected item and the remove that item from the array. You could then add that removed item into a different "holding" array.

Then, when you select the next quote, it would be from the reduced array containing only those that have not yet been printed. You could repeat this until the array is empty, and at that time you could copy the entire "holding" array back into the "working" one and start the process over.

Does that sound like a good strategy that you could implement?

Yes, that is actually what i had in mind, but my problem is knowing how to write it in code. Especially the second part, where i have to copy the new array into the original array so that it can start the process over.

Steven Parker
Steven Parker
243,266 Points

Putting it back is actually the easy part:

Questions = Holding;  // copy the used questions back to the original array
Holding = [];         // and reset the holding area to an empty array
//-------------------- OR --------------------
Questions = Holding.splice(0, Holding.length);  // move the questions back

Give it a shot, if you have problems with it post your code here (or even better, make a snapshot of your workspace and post the link to it here).

var holding = []; /* new quote array container */

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

quotes.splice(randomNumber, 1); /* remove item from original array */
holding.push(quote); /* add quote to new container */

/* how do I create a cycle, when the quotes array become empty, it goes to the new holding array instead, and so on */

return quote;
}

this is where i got stuck on, trying to create that cycle

Steven Parker
Steven Parker
243,266 Points

Just check for an empty list and reset it as first thing in the function:

function getRandomQuote() {
  if (quotes.length == 0) {    // start over if all have been used
    quotes = holding;  holding = [];
  }
  //...

Happy coding!

ahhh thank you so much, got it