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.
JavaScriptx31for (var i = 0; i < roa.length; i++) {questionContentRoa = roa[i].questionContent, correctAnswerRoa = roa[i].correctAnswer }
2console.log(questionContentRoa, correctAnswerRoa);
3
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.
JavaScript
1
6
1
for (var i = 0; i < roa.length; i++) {
2
questionContentRoa = roa[i].questionContent;
3
correctAnswerRoa = roa[i].correctAnswer;
4
}
5
console.log(questionContentRoa, correctAnswerRoa);
6