im selecting a statement from a json file, but the one i get is done by a randome number, but i want to avoid getting the same one consecutive times, is there a way to limit this?
Right now this is the code i have for that part, it gives me a random question beetween the amounth of questions i have
const json = JSON.parse(fs.readFileSync('quiz.json'));
const quizNum = Math.floor(Math.random() * 22);
i was thinking something to avoid getting the same question if it was given in the last 2 or 3 times
2
Answers
I would do an array to keep track of the recent questions and adjust with a variable the length of that array
and then do a function that will check if the new question is in the array of recentQuestions
and if yes, proceed by doing a
to remove the oldest question in recentQuestions
and push the new question with a
Here is a working snippet, demonstrating how you can pick some random questions, based on a shuffled index array: