Array not returned properly

You must Login before you can answer or comment on any questions.

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

2 Answers

Accepted Answer

hi,

the only tweak is you are resetting counter to 0 in each iteration so declare this only once..

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;
}

alert(dataArray[counter]);//Showing two values

How can this show two values?

Your Answer

Think you can help? Login to answer this question!