I’m pretty stuck on Random Quote Machine. I couldn’t get an API for quotes to cooperate with CodePen so I have written functional JavaScript (JS tested and ran successfully in my browser console, anyway) to return a quote selected at random from an array. The problem I am currently having is that I cannot seem to bring this quote into my jQuery, so that this quote returns on button click. What I am getting instead is “0”. Here is the link to my CodePen. http://codepen.io/Mmgfrog/pen/egOxwq
Please advise. Thank you!
PS - I haven’t tried the Twitter API yet so please overlook that part for now. Want to get quote working correctly first.
Hi @Mmgfrog
Instead of returning "<b>" + quote + "</b>"
, you probably want to call your randomQuote
function like: "<b>" + randomQuote() + "</b>"
. This will get the correct return value from the array.
As an aside, you may want to consider using .html
instead of .append
. So that it replaces the previous quote, rather than appending it to the h4.
You’re almost there. You do not need to declare randomQuote()
- you can do everything inside .append()
's anonymous function.
$("h4").append(function(quote) {
function randomQuote() { ... // <- don't need me!
And you don’t need the while
loop. Keep the random number generator, but instead of hard-coding the upper bound, you can generate a random number between 0
and arr.length
- that will select a quote from any size array.
You’re on the right track.
Edit to agree with @joesmith100: you want .html()
instead of .append()
Thank you so much @joesmith100 and @belcurv. I implemented your suggestions and it’s working great now. Sweet! I appreciate your help.