I can’t seem to get my tweet text to come up as anything but undefined, even though I’ve declared the variables outside my getQuote function! My quote function works just fine. Here’s my codepen:
Thank you I really appreciate the help! Alot of other people that had my problem seemed to fix it by declaring the variables outside of the function, but its still not working for me for some reason.
The problem you’re having is related to the scope of
Because your defining ranQuote again inside the getQuote function, it’s assigning the random quote to that variable inside the function making it local to getQuote and not available outside of it. So when the button reads ranQuote, it will always be undefined.
Easiest way to fix it is remove the var declaration from ranQuote and ranAuthor inside the getQuote function, that way it will overwrite the variables in it’s upper scope, the ready function.
var quotes = [...];
var random = Math.floor(Math.random()*quotes.length);
// Now these two will override the variables in the ready function and be available to the tweet button.
ranQuote = quotes[random];
ranAuthor = author[random];
Thank you so much I get it now!!