MCPcopy Create free account
hub / github.com/freeCodeCamp/freeCodeCamp / generateRandomExam

Function generateRandomExam

api/src/utils/exam.ts:28–55  ·  view source on GitHub ↗
(examJson: Exam)

Source from the content-addressed store, hash-verified

26 * @returns An array of randomized questions for the exam.
27 */
28export function generateRandomExam(examJson: Exam): GeneratedExam {
29 const { numberOfQuestionsInExam, questions } = examJson;
30 const numberOfAnswersPerQuestion = 5;
31
32 const availableQuestions = shuffleArray(filterDeprecated(questions));
33 const examQuestions = availableQuestions.slice(0, numberOfQuestionsInExam);
34
35 const randomizedExam: GeneratedExam = examQuestions.map(
36 (question: Question) => {
37 const availableCorrectAnswers = filterDeprecated(question.correctAnswers);
38 const availableWrongAnswers = shuffleArray(
39 filterDeprecated(question.wrongAnswers)
40 );
41 const correctAnswer = getRandomElement(availableCorrectAnswers);
42 const answers = shuffleArray([
43 correctAnswer,
44 ...availableWrongAnswers.slice(0, numberOfAnswersPerQuestion - 1)
45 ]).map(({ id, answer }) => ({ id, answer }));
46 return {
47 id: question.id,
48 question: question.question,
49 answers
50 };
51 }
52 );
53
54 return randomizedExam;
55}
56
57/**
58 * Evaluates a user completed exam.

Callers 2

exam.test.tsFile · 0.85
challengeRoutesFunction · 0.85

Calls 3

shuffleArrayFunction · 0.90
filterDeprecatedFunction · 0.85
getRandomElementFunction · 0.85

Tested by

no test coverage detected