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 trialterrenceg
Courses Plus Student 3,380 PointsHow would I generate the next value in an array instead of a random one for my fun facts app?
when I click my fact button I would like to see the next fact in my array instead of a random one.
1 Answer
jcorum
71,830 Pointsterrence, here are the basic steps:
Create a class variable counter and initialize it to 0.
Change fact = facts[randomNumber]; to fact = facts[counter].
Increment counter each time you click the fact button: counter += 1
Then add this conditional, just after you increment counter, to make sure you don't increment too far: if counter > facts.count { counter = 0; } This resets counter to 0, to start over.