From 88060543132184d817e3fe7b784ddc2678288e50 Mon Sep 17 00:00:00 2001 From: Manal Malik Date: Mon, 27 Jul 2026 17:44:11 +0200 Subject: [PATCH 1/3] Added quiz and the question logic --- src/question.js | 19 ++++++++++++++----- src/quiz.js | 45 +++++++++++++++++++++++++++++++++++---------- 2 files changed, 49 insertions(+), 15 deletions(-) diff --git a/src/question.js b/src/question.js index 68f6631a..f12e8fa2 100644 --- a/src/question.js +++ b/src/question.js @@ -1,7 +1,16 @@ class Question { - // YOUR CODE HERE: - // - // 1. constructor (text, choices, answer, difficulty) + constructor(text, choices, answer, difficulty) { + this.text = text; + this.choices = choices; + this.answer = answer; + this.difficulty = difficulty; + } - // 2. shuffleChoices() -} \ No newline at end of file + shuffleChoices = () => { + for (let i = this.choices.length - 1; i > 0; i--) { + const j = Math.floor(Math.random() * (i + 1)); + [this.choices[i], this.choices[j]] = [this.choices[j], this.choices[i]]; + } + return this.choices; + }; +} diff --git a/src/quiz.js b/src/quiz.js index d94cfd14..d1ece457 100644 --- a/src/quiz.js +++ b/src/quiz.js @@ -1,15 +1,40 @@ class Quiz { - // YOUR CODE HERE: - // - // 1. constructor (questions, timeLimit, timeRemaining) + constructor(questions, timeLimit, timeRemaining) { + this.questions = questions; + this.timeLimit = timeLimit; + this.timeRemaining = timeRemaining; + this.correctAnswers = 0; + this.currentQuestionIndex = 0; + } - // 2. getQuestion() - - // 3. moveToNextQuestion() + getQuestion = () => { + console.log(this.questions); + return this.questions[this.currentQuestionIndex]; + }; - // 4. shuffleQuestions() + moveToNextQuestion = () => { + this.currentQuestionIndex++; + }; - // 5. checkAnswer(answer) + shuffleQuestions = () => { + for (let i = this.questions.length - 1; i > 0; i--) { + const j = Math.floor(Math.random() * (i + 1)); + [this.questions[i], this.questions[j]] = [ + this.questions[j], + this.questions[i], + ]; + } + return this.questions; + }; - // 6. hasEnded() -} \ No newline at end of file + checkAnswer = (answer) => { + let question = this.getQuestion(); + if (answer === question.answer) { + return this.correctAnswers++; + } + }; + + hasEnded = () => { + return this.currentQuestionIndex >= this.questions.length; + }; +} From 17a628f23110b2884dbc415d69e4f1c41d888d40 Mon Sep 17 00:00:00 2001 From: Manal Malik Date: Tue, 28 Jul 2026 15:21:38 +0200 Subject: [PATCH 2/3] Day 02: JS Project Completed --- src/quiz.js | 17 ++++++++++++++++- tests/quiz.spec.js | 1 + 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/quiz.js b/src/quiz.js index d1ece457..c82b4713 100644 --- a/src/quiz.js +++ b/src/quiz.js @@ -37,4 +37,19 @@ class Quiz { hasEnded = () => { return this.currentQuestionIndex >= this.questions.length; }; -} + + filterQuestionsByDifficulty = (difficulty) => { + if (typeof difficulty !== "number" || difficulty < 1 || difficulty > 3) { + return this.questions; + } + + this.questions = this.questions.filter((question) => question.difficulty === difficulty); + return this.questions; + }; + + averageDifficulty = () => { + let difficulties = this.questions.map((question) => question.difficulty) + let sumOfDifficulties = difficulties.reduce((accum, currentValue) => accum + currentValue, 0) + return sumOfDifficulties / difficulties.length + } +} \ No newline at end of file diff --git a/tests/quiz.spec.js b/tests/quiz.spec.js index 88a723bd..f4b66dbf 100644 --- a/tests/quiz.spec.js +++ b/tests/quiz.spec.js @@ -313,6 +313,7 @@ describe("Quiz", () => { // Call the `filterQuestionsByDifficulty()` method with a number between 1 and 3 as a 1st argument. quiz.filterQuestionsByDifficulty(2); + console.log(quiz.questions, "inside test"); // Check if the questions array has been filtered correctly expect(quiz.questions).toEqual([questions[1], questions[2]]); From 130e3400fe119bea163d8dd1fb69b4942841aff1 Mon Sep 17 00:00:00 2001 From: Manal Malik Date: Tue, 28 Jul 2026 15:23:26 +0200 Subject: [PATCH 3/3] Day 02: JS Project Completed --- tests/quiz.spec.js | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/quiz.spec.js b/tests/quiz.spec.js index f4b66dbf..88a723bd 100644 --- a/tests/quiz.spec.js +++ b/tests/quiz.spec.js @@ -313,7 +313,6 @@ describe("Quiz", () => { // Call the `filterQuestionsByDifficulty()` method with a number between 1 and 3 as a 1st argument. quiz.filterQuestionsByDifficulty(2); - console.log(quiz.questions, "inside test"); // Check if the questions array has been filtered correctly expect(quiz.questions).toEqual([questions[1], questions[2]]);