3

I have a empty array named questions and I used the $.get method to add objects to the array. inside $.get, array is filled with objects and works fine. but once outside of $.get, array becomes empty again, as array.length returns 0.

var questions=[];
var input;
$(document).ready(function(){
    $.get('songs.html', function(list){
        var cat = $(list).find('li');
        var dog = [];
        var count = 0;
        var n;
        cat.each(function(i, li){
                dog[count] = $(li).text();
                count = count + 1;
                if(count == 3){
                    n = Math.floor(Math.random()*4);
                    questions.push({"song":"","choice1":dog[0],"choice2":dog[1],"choice3":dog[2],"choice4":dog[3], "answer":dog[n]});
                    count = 0;
                }
        });

        console.log(questions.length);//array has length
    });
    console.log(question.length);
Bango
  • 288
  • 2
  • 14
  • It's passed by reference. But you are checking array length before it's populated with data. – dfsq Nov 01 '15 at 21:00
  • I used`console.log` after the `each` function. inside of `$.get` , the length is 333 (the number I was expecting), but otuside of `$.get` the length returns 0 – Bango Nov 01 '15 at 21:05
  • You're not actually "passing" the array anywhere. You're trying to output it before it has any values. – JLRishe Nov 01 '15 at 21:38

0 Answers0