JS ninja needed - Twitter debug on random quote generator

I am stuck with the twitter part. I can’t share the random quote to twitter. Im sure that the format for the quote and author cause the issue?

Thanks in advanced

It’s not the “format of the quote”. The problem is you defined your quote and author variables inside $.getJSON's callback function. The scope of a variable declared with var is its current execution context; your callback function in this case. It means, these variables are not available outside of this function.

Not just this, you also use an asynchronous call ($.getJSON) to get the quotes. Even if you declared your variables outside of $.getJSON, they wouldn’t get updated by the time you call $("#twit").on("click", ....

I think you should google how async JS and callbacks work. You’ll really need these, especially if you want to do the backend projects.

1 Like