I am trying to learn JS by building a random Quote Machine but this problem has been bugging me. I saw the other answers but I really couldn't understand them due to lack of context. Any help will be appreciated. The code is:
$(document).ready(function () {
$("#btn").click(function () {
getQuote(); //This should execute first, then the next lines
var quoteData = jSonify();
var quote = quoteData[0];
var author = quoteData[1];
console.log(quote);
console.log(author);
console.log("Button Clicked");//This Should execute last.
});
//Get them quotes
function getQuote() {
$.ajax({
url: 'https://andruxnet-random-famous-quotes.p.mashape.com/?cat=famous',
type: 'GET',
data: {},
datatype: 'json',
success: function (data) { jSonify(data); },
error: function (err) { alert(err); },
beforeSend: function (xhr) {
xhr.setRequestHeader("X-Mashape-Authorization", "qKPbfOzWKemsh2qi30QgbOA1WufXp1ok1NsjsnAkvh6yVJfaAk");
}
});
}
//Convert to jSon
function jSonify(rawData) {
var jSonified = jQuery.parseJSON(rawData);
var quote = jSonified.quote;
var author = jSonified.author;
console.log(quote);
console.log(author);
return [quote, author];
}});