This may appear to be a small and insignificant issue, but I am struggling to find a solution.
Within this function, var q
is set to an array of strings. When the function is called, alert(q)
successfully displays the entire array.
function initializeQuiz() {
$.post("quiz.php", { 'func': 'load' }, function(data) {
var q = data.split(".\n");
alert(q);
return q;
});
}
However, when attempting to utilize the function (as shown below in 2 different ways), I receive an error stating that the array is undefined. Why is this happening?
var quizQuestions;
$(function() {
//This method does not work
quizQuestions = initializeQuiz();
alert(quizQuestions);
//Neither does this one
alert(initializeQuiz());
});
After conducting further investigation, I included a callback in initializeQuiz()
, but encountered the same outcome.