Search code examples
javascriptandroidiphonearraystitanium

Array not returned properly(Titanium)


I am returning the array using a function in titanium.There are two entries in my array which are showing alert but when i access the returned value in another js file.It only show one value in alert Here is my code (it is in db.js):

function quizfun() {
    var dataArray=new Array();
    var quizes = db.execute('select * from Quiz');
    while (quizes.isValidRow()) {
        var counter = 0;
        dataArray[counter] = quizes.fieldByName('Quiz_Text');
        quizes.next();
        alert(dataArray[counter]);//Showing two values
        counter++;
    };
    return dataArray;
}

Here is my other js file:

  quizes = db.quizfun();
  alert(quizes[0]);//working
  alert(quizes[1]);//alert not showing anything

Could you tell me what i am doing wrong.Thanks in advance


Solution

  • var counter = 0; should be outside the while loop. Like

    function quizfun() {
        var dataArray=new Array();
        var quizes = db.execute('select * from Quiz');
        var counter = 0;
        while (quizes.isValidRow()) {
            dataArray[counter] = quizes.fieldByName('Quiz_Text');
            quizes.next();
            alert(dataArray[counter]);//Showing two values
            counter++;
        };
        return dataArray;
    }