I am writing a function for my node/angular app that will prompt the user with a random question that he was not asked before.
To achieve this I wrote this function:
function setQuestion() {
var q; // question we will eventually ask the user
for (q in questions) {
if (!(q._id in questionsUserAlreadyAnswered)) {
/* the user has NOT answered this question yet -> so we can ask him now! */
$scope.title = q.description;
$scope.questionToAsk = q;
return;
}
}
/* if we got here it means the user has already answered ALL the questions */
$scope.title = 'None';
$scope.questionToAsk = 'None';
}
Here questions
is an array of questions (Strings
) and so is questionsUserAlreadyAnswered
.
I am wondering if this is efficient enough, since I have many questions in the database and the user possibly answered many of them before, so essentially I iterate over all the questions I asked him.(?) twice.