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

cathymacars
seal-mask
.a{fill-rule:evenodd;}techdegree
cathymacars
Full Stack JavaScript Techdegree Student 15,941 Points

How to ...?

...

You could have a variable that keeps track of which images are currently being displayed.

If an image is currently displayed, skip to the next available one.

1 Answer

Steven Parker
Steven Parker
243,656 Points

There are a couple of approaches that would work here.

One would be to check each random pick against the list of previously chosen items, and if found, discard it and pick another. Since you're picking less than half of the total set this should be reasonably efficient.

Another way would be to start with a list of available choices, and as each random pick is made, remove the chosen item from the list. Then the next choice would be made from the reduced set. This method works well even when picking every item of the set.