diff --git a/controllers/historyController.js b/controllers/historyController.js index 55a9dc9..f322ff9 100644 --- a/controllers/historyController.js +++ b/controllers/historyController.js @@ -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); @@ -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) { diff --git a/package.json b/package.json index fcbc220..93d191d 100644 --- a/package.json +++ b/package.json @@ -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"