Skip to content

Devon's #236

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import config from 'eslint-config-xo';
import {defineConfig} from 'eslint/config';

export default defineConfig([
config,
]);
28 changes: 28 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
25 changes: 20 additions & 5 deletions src/question.js
Original file line number Diff line number Diff line change
@@ -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;
}
}
89 changes: 76 additions & 13 deletions src/quiz.js
Original file line number Diff line number Diff line change
@@ -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()
}
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;
}
}