Random quote machine: Quote does not always load

Random quote machine: Quote does not always load
0

#1

Hey everyone,

I’ve finally finished the basic functionality of my quote machine, but I have a problem which I haven’t been able to solve. I can’t seem to find a solution in the forum or elsewhere either. The code works ok, but sometimes the quote won’t load. This happens both at page load/refresh, and on button click. Could anyone help me figure this out? I realize that we should probably use an API for this, and that will be my next task, but I would still like to understand why this is happening.

Pen link: https://codepen.io/cestbalez/pen/zPPzzm

Thanks for any help.

Magnus


#2

Shouldn’t this be var randNumber=Math.floor(Math.random()*quotes.length/2);?
Next you use quotes[randNumber*2], which overshoots the length of quotes in certain cases (when Math.random()>0.5).


#3

You seem to have your quotes stored in an array, so I guess the bug comes from JS giving you the same random number, thus showing you the same quote.

P.S.

Don’t get me wrong but I think the goal of Random Quote Machine project was to learn how to get data using AJAX request, instead of writing the quotes yourself. :wink:


#4

Yes, that’s it! I suspected it was something silly like that. Thanks!


#5

Actually Dan_Cio found the mistake. Thanks anyways though. And yeah, like I stated in the text, I will get to using AJAX request next :slight_smile:


#6

There’s no such requirement. There’re only 2 requirements which are clearly stated in challenge description. (and remember, you’re not supposed to look at example project’s code)