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
Benjamin Thai
Full Stack JavaScript Techdegree Graduate 20,128 Pointshow 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.
4 Answers
Steven Parker
243,266 PointsThere 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?
Benjamin Thai
Full Stack JavaScript Techdegree Graduate 20,128 PointsYes, 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
243,266 PointsPutting 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).
Benjamin Thai
Full Stack JavaScript Techdegree Graduate 20,128 Pointsvar 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;
}
Benjamin Thai
Full Stack JavaScript Techdegree Graduate 20,128 Pointsthis is where i got stuck on, trying to create that cycle
Steven Parker
243,266 PointsJust 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!
Benjamin Thai
Full Stack JavaScript Techdegree Graduate 20,128 Pointsahhh thank you so much, got it
Benjamin Thai
Full Stack JavaScript Techdegree Graduate 20,128 PointsBenjamin Thai
Full Stack JavaScript Techdegree Graduate 20,128 Pointscant figure this out, been trying all day, please help