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 Can I Make This More Efficient

Hi Guys,

I have just finished creating a random quote generator. It has its flaws here and there, and I was just wondering how could I make this more efficient?

Here is my CodePen : https://codepen.io/Nathan-Callum-Marshall/pen/rrapGQ

Thanks,

2 Answers

You have a lot of duplicated code. This comes because you're trying to hide different blocks of code instead of inserting a singe block.

In other words, if you stored the quotes in an array, you could use a random number generator to create the index (and since it's 0 based, you can then lose the +1 in your random number code), return the string, and insert it into the code block.

So in its current state Nathan's project could get by with one array and one function, right?

I will give this a try. Thank you Lucas

Mike Hatch Yes, definitely

Lukas DahlbergI. I have tried your solution. What would you approach be to show one array item at a time?