diff --git a/eslint.config.mjs b/eslint.config.mjs new file mode 100644 index 00000000..2cf0ebb7 --- /dev/null +++ b/eslint.config.mjs @@ -0,0 +1,6 @@ +import config from 'eslint-config-xo'; +import {defineConfig} from 'eslint/config'; + +export default defineConfig([ + config, +]); diff --git a/package.json b/package.json new file mode 100644 index 00000000..545ea965 --- /dev/null +++ b/package.json @@ -0,0 +1,28 @@ +{ + "name": "javascript-quiz-project", + "version": "1.0.0", + "description": "", + "main": "index.js", + "directories": { + "test": "tests" + }, + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/TheZuckaNator/javascript-quiz-project.git" + }, + "author": "", + "license": "ISC", + "bugs": { + "url": "https://github.com/TheZuckaNator/javascript-quiz-project/issues" + }, + "homepage": "https://github.com/TheZuckaNator/javascript-quiz-project#readme", + "devDependencies": { + "@eslint/js": "^9.26.0", + "eslint": "^9.26.0", + "eslint-config-xo": "^0.47.0", + "globals": "^16.1.0" + } +} diff --git a/src/question.js b/src/question.js index 68f6631a..720ac724 100644 --- a/src/question.js +++ b/src/question.js @@ -1,7 +1,22 @@ class Question { - // YOUR CODE HERE: - // - // 1. constructor (text, choices, answer, difficulty) - - // 2. shuffleChoices() + constructor(text, choices, answer, difficulty) { + this.text = text; + this.choices = choices; + this.answer = answer; + this.difficulty = difficulty; + } + + shuffleChoices() { + // Fisher-Yates shuffle algorithm + const shuffled = [...this.choices]; // Create a copy of the array + + for (let i = shuffled.length - 1; i > 0; i--) { + // Pick a random index from 0 to i + const j = Math.floor(Math.random() * (i + 1)); + // Swap elements at indices i and j + [shuffled[i], shuffled[j]] = [shuffled[j], shuffled[i]]; + } + + this.choices = shuffled; + } } \ No newline at end of file diff --git a/src/quiz.js b/src/quiz.js index d94cfd14..c90e2a7a 100644 --- a/src/quiz.js +++ b/src/quiz.js @@ -1,15 +1,78 @@ class Quiz { - // YOUR CODE HERE: - // - // 1. constructor (questions, timeLimit, timeRemaining) - - // 2. getQuestion() + constructor(questions, timeLimit, timeRemaining) { + this.questions = questions; + this.timeLimit = timeLimit; + this.timeRemaining = timeRemaining; + + // Initialize additional properties with default values + this.correctAnswers = 0; + this.currentQuestionIndex = 0; + } + + getQuestion() { + // Return the current question based on the currentQuestionIndex + return this.questions[this.currentQuestionIndex]; + } - // 3. moveToNextQuestion() - - // 4. shuffleQuestions() - - // 5. checkAnswer(answer) - - // 6. hasEnded() -} \ No newline at end of file + moveToNextQuestion() { + // Increment the currentQuestionIndex by 1 + this.currentQuestionIndex += 1; + } + + shuffleQuestions() { + // Create a copy of the questions array to avoid modifying the original + const shuffled = [...this.questions]; + + // Fisher-Yates shuffle algorithm + for (let i = shuffled.length - 1; i > 0; i--) { + // Pick a random index from 0 to i + const j = Math.floor(Math.random() * (i + 1)); + // Swap elements at indices i and j + [shuffled[i], shuffled[j]] = [shuffled[j], shuffled[i]]; + } + + // Update the questions property with the shuffled array + this.questions = shuffled; + } + + checkAnswer(answer) { + const currentQuestion = this.getQuestion(); + + // Check if the provided answer matches the correct answer for the current question + if (currentQuestion && answer === currentQuestion.answer) { + this.correctAnswers += 1; + return true; + } + return false; + } + + hasEnded() { + // Check if we've reached the end of the questions + return this.currentQuestionIndex >= this.questions.length; + } + + filterQuestionsByDifficulty(difficulty) { + // Check if difficulty is valid (between 1 and 3) + if (typeof difficulty !== 'number' || difficulty < 1 || difficulty > 3) { + return; // Do nothing if difficulty is invalid + } + + // Filter questions by the specified difficulty + this.questions = this.questions.filter(question => question.difficulty === difficulty); + } + + averageDifficulty() { + // If there are no questions, return 0 to avoid division by zero + if (this.questions.length === 0) { + return 0; + } + + // Use reduce() to sum all difficulty values + const totalDifficulty = this.questions.reduce((sum, question) => { + return sum + question.difficulty; + }, 0); + + // Calculate and return the average + return totalDifficulty / this.questions.length; + } + } \ No newline at end of file