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 randomly cycle through an array?

Hi all!

I'm working on a FreeCodeCamp challenge as well as Treehouse right now. It's called the Random Quote Machine. When I click a button, it makes a new quote appear. I have the basic markup and styling, but some functionality is giving me trouble. I have it so far, where if I click the button, some text pops up through jQuery, but not one of the random quotes in my array. (I'm allowed to use an array or API for this.) What I need to do next, is, when I click a button, have one of my quotes in the array appear. I logged this to the console, but it is returning 'undefined'. Please DO NOT give me a direct solution, but would a good hint be out there? Just a little stuck and frustrated.

Thanks a lot, ~ G

1 Answer

Micah Murray
Micah Murray
6,913 Points

I'll give you one hint, try generating a random number inclusively between the lower and upper limit of your array size.

Thanks Micah Murray! I think I'm getting it a bit more now. I'm just not sure how to define the min and max values. Should I post a link to the pen so you can see if you'd like?

Micah Murray
Micah Murray
6,913 Points

Sure Gabriel, that would be fine.

Micah Murray
Micah Murray
6,913 Points

I had a look at your code and it looks good so far. However, you should use a function for getting the random number and for getting the quote. Also, there is a problem with your random number code.

Ok, that's what I thought. I will be able to figure the rest out for myself if I can only figure this out: just how to print out my first quote to the page. Forget about the random factor for now. How come that last line isn't printing, the one that has the .html method?

Micah Murray
Micah Murray
6,913 Points

It is printing at least as far as I can see. But Its not printing the quote for two reasons. First, the variable that stores the quote to be displayed is commented out. Secondly, once you uncomment that line the random number code needs to be fixed.