Skip to content

Commit

Permalink
fisher-yates algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
RickyRAV committed Jun 4, 2024
1 parent 1914b5a commit 0a82e5d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
10 changes: 10 additions & 0 deletions controllers/historyController.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ const userHistory = async (req, res) => {
}
});

// Fisher-Yates (Knuth) shuffle algorithm
const shuffleArray = (array) => {
for(let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[array[i], array[j]] = [array[j], array[i]];
}
}

// extract topics and flatten the array
const topics = allQuizzes.flatMap(quiz => quiz.topics);

Expand All @@ -56,6 +64,8 @@ const userHistory = async (req, res) => {
}));

const totalCount = await prisma.quizzes.count({where: {user_id}});
shuffleArray(topFiveTopics)
console.log(topFiveTopics);

res.json({quizzes: formattedQuizzes, totalCount, topFiveTopics});
} catch (error) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"author": "",
"scripts": {
"test": "vitest",
"dev": "nodemon -r dotenv/config ./index.js dotenv_config_path=./.env.development",
"dev": "nodemon -r dotenv/config ./index.js dotenv_config_path=./.env.production",
"db-introspect": "dotenv -e .env.development -- prisma generate",
"db-generate": "dotenv -e .env.development -- prisma db pull",
"db-sync": "dotenv -e .env.tests -- prisma db push"
Expand Down

0 comments on commit 0a82e5d

Please sign in to comment.