Suggestions to optimize Random Quote generator

Hey all. I got a pretty rudimentary random quote generator here:
I’m pretty new to using javascript so it was the best I could come up with. I generated a random number and used a switch case to assign the quote to a quote parameter based on the number selected. I’m sure there are other ways to do this and so I was looking for suggestions to make this better and faster (without using API’s because I have no clue how to utilize that right now). Otherwise I’ll just keep hardcoding quotes in there. Any other recommendations are welcome. Cheers!!

It is a pretty simple task so fon’t worry about optimizing for speed. You could however use an array instead of a switch statement.

Store the quotes in the array.
Generate the random number such that it is between 0 and the length of the array minus one.
Retrieve the quote from the array by index.

That should make the code look a bit better.

Oh great Idea! I remember in another post someone talking about using objects. Any clue how that would be done?

Well you could use an object as an associative array. This is because you can iterate through its props with a for…in statemment. But its more work than an array in your case.

After a journey in which I rediscovered myself, scoured a lot of youtube videos, dabbled in some php, reupped my jquery, and googled “what is an api and why can’t I figure out how to use it” I finally cracked it thanks to fcc,codecademy,udemy,stack overflow and a very cool youtube ( video.
This is it by the way

P.S. Now that I get it, it’s actually pretty easy haha just had to cut through all the noise.

1 Like