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

Function createExamResults

api/src/utils/exam.ts:64–103  ·  view source on GitHub ↗
(userExam: UserExam, originalExam: Exam)

Source from the content-addressed store, hash-verified

62 * @returns An object of the exam results.
63 */
64export function createExamResults(userExam: UserExam, originalExam: Exam) {
65 const { userExamQuestions, examTimeInSeconds } = userExam;
66 const {
67 questions: originalQuestions,
68 numberOfQuestionsInExam,
69 passingPercent
70 } = originalExam;
71
72 const numberOfCorrectAnswers = userExamQuestions.reduce(
73 (count, userQuestion) => {
74 const originalQuestion = originalQuestions.find(
75 examQuestion => examQuestion.id === userQuestion.id
76 );
77
78 if (!originalQuestion) {
79 throw new Error('An error occurred. Could not find exam question.');
80 }
81
82 const isCorrect = originalQuestion.correctAnswers.find(
83 examAnswer => examAnswer.id === userQuestion.answer.id
84 );
85 return isCorrect ? count + 1 : count;
86 },
87 0
88 );
89
90 // Percent rounded to one decimal place
91 const percent = (numberOfCorrectAnswers / numberOfQuestionsInExam) * 100;
92 const percentCorrect = Math.round(percent * 10) / 10;
93 const passed = percentCorrect >= passingPercent;
94
95 return {
96 numberOfCorrectAnswers,
97 numberOfQuestionsInExam,
98 percentCorrect,
99 passingPercent,
100 passed,
101 examTimeInSeconds
102 };
103}

Callers 2

exam.test.tsFile · 0.85
challengeRoutesFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected