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..c82b4713 100644 --- a/src/quiz.js +++ b/src/quiz.js @@ -1,15 +1,55 @@ 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() + checkAnswer = (answer) => { + let question = this.getQuestion(); + if (answer === question.answer) { + return this.correctAnswers++; + } + }; + + 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