Skip to content
Advertisement

For loop only returns last number [closed]

I have made a for loop in order to console log multiple entries in an array. The for loop, however, only returns the last entry in the array, instead of everything from 0 to end of array.

for (var i = 0; i < roa.length; i++) {questionContentRoa = roa[i].questionContent, correctAnswerRoa = roa[i].correctAnswer }
                console.log(questionContentRoa, correctAnswerRoa);

Advertisement

Answer

It will be clearer to you if you ident the code a little bit.

The console.log is outside of the scope, hence, it’s only logging the last assignment before the loop ends.

for (var i = 0; i < roa.length; i++) {
    questionContentRoa = roa[i].questionContent;
    correctAnswerRoa = roa[i].correctAnswer;
}
console.log(questionContentRoa, correctAnswerRoa);
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement