This seems like a ridiculously trivial issue but I can't solve it.
I have this function. var q
is assigned to an array of strings. alert(q)
successfully prints the entire array when the function is called.
function initQuestions() {
$.post("quiz.php", { 'func': 'load' }, function(data) {
var q = data.split(".\n");
alert(q);
return q;
});
}
However, when I attempt to use the function (as depicted below in 2 ways) I am told the array is undefined. Why is this the case?
var questions;
$(function() {
//This doesn't work
questions = initQuestions();
alert(questions);
//Neither does this
alert(initQuestions());
});
Upon further research I added a callback to initQuestions()
but am having the same result.