I have some items in firestore.I want to show them sequentially but in random serial. So, I did the following code. Here I want to show a specific question and then when "next" button is clicked I want to show the next item. Here, in the forEach loop, I am rendering items to the renderQuestion function. And they are showing them in frontend using HTML and CSS.
But I am fetching this issue, where they keep showing everything till the end of the loop. They don't wait for the "next" button to be pressed. So, how can I fix this? I am new to JS, please forgive me, if I am already doing something wrong.
renderQuestion = (qID, roomID) => {
q.innerHTML = qID.data().question;
ans1.innerHTML = qID.data().a;
ans2.innerHTML = qID.data().b;
ans3.innerHTML = qID.data().c;
ans4.innerHTML = qID.data().d;
/* next.addEventListener("click", (e) => { //not working
e.preventDefault();
console.log("done");
}); */
};
runningExam = async (review) => {
const totalQ = review.data().total_questions;
let arr = [];
for (let i = 0; i <= totalQ; i++) {
arr.push(false);
}
let n = totalQ;
while (n) {
let randomNumber = getRandom(totalQ);
if (!arr[randomNumber]) {
arr[randomNumber] = true;
await db
.collection("examrooms")
.doc(review.id)
.collection("questions")
.where("serial", "==", randomNumber)
.get()
.then((snapshot) => {
snapshot.docs.forEach((doc) => {
renderQuestion(doc, review);
});
});
n--;
}
}
};