diff --git a/index.html b/index.html index 534cd886..ac8bd319 100644 --- a/index.html +++ b/index.html @@ -18,7 +18,7 @@

JavaScript Quiz

- +
@@ -39,7 +39,7 @@

JavaScript Quiz

- + diff --git a/src/index.js b/src/index.js index 03737ba3..d6ec7f8e 100644 --- a/src/index.js +++ b/src/index.js @@ -98,39 +98,49 @@ document.addEventListener("DOMContentLoaded", () => { // // 1. Show the question // Update the inner text of the question container element and show the question text + questionContainer.innerText = question.text; // 2. Update the green progress bar // Update the green progress bar (div#progressBar) width so that it shows the percentage of questions answered - progressBar.style.width = `65%`; // This value is hardcoded as a placeholder + let percentage = (100/quiz.questions.length) * quiz.currentQuestionIndex; + progressBar.style.width = `${percentage}%`; // This value is hardcoded as a placeholder // 3. Update the question count text // Update the question count (div#questionCount) show the current question out of total questions - questionCount.innerText = `Question 1 of 10`; // This value is hardcoded as a placeholder + //#questionCount = current.question of questions.length + let questionCount = quiz.currentQuestionIndex +1; + questionCount.innerText = `${questionCount} of ${quiz.questions.length}`; // This value is hardcoded as a placeholder // 4. Create and display new radio input element with a label for each choice. // Loop through the current question `choices`. + + + question.choices.forEach(choice => { + // pour chaque choix, on crée un radio input + choiceContainer.innerHTML+= ` + +
`; + // For each choice create a new radio input with a label, and append it to the choice container. // Each choice should be displayed as a radio input element with a label: /* - - -
+ */ // Hint 1: You can use the `document.createElement()` method to create a new element. // Hint 2: You can use the `element.type`, `element.name`, and `element.value` properties to set the type, name, and value of an element. // Hint 3: You can use the `element.appendChild()` method to append an element to the choices container. // Hint 4: You can use the `element.innerText` property to set the inner text of an element. + }); } - function nextButtonHandler () { let selectedAnswer; // A variable to store the selected answer value @@ -140,13 +150,28 @@ document.addEventListener("DOMContentLoaded", () => { // YOUR CODE HERE: // // 1. Get all the choice elements. You can use the `document.querySelectorAll()` method. - + let allChoice = document.querySelectorAll("input"); + // 2. Loop through all the choice elements and check which one is selected // Hint: Radio input elements have a property `.checked` (e.g., `element.checked`). // When a radio input gets selected the `.checked` property will be set to true. // You can use check which choice was selected by checking if the `.checked` property is true. - + allChoice.forEach((select) => { + console.log(select.checked); + if (select.checked) { + selectedAnswer = select.value; + }; + }); + + if (selectedAnswer) { + //1er hint : appel la méthode avec selectedanswer + quiz.checkAnswer(selectedAnswer); + //2eme hint : + quiz.moveToNextQuestion(); + //3eme hint : + showQuestion(); + } // 3. If an answer is selected (`selectedAnswer`), check if it is correct and move to the next question // Check if selected answer is correct by calling the quiz method `checkAnswer()` with the selected answer. @@ -168,7 +193,34 @@ document.addEventListener("DOMContentLoaded", () => { endView.style.display = "flex"; // 3. Update the result container (div#result) inner text to show the number of correct answers out of total questions - resultContainer.innerText = `You scored 1 out of 1 correct answers!`; // This value is hardcoded as a placeholder + + const resultContainer = document.querySelector("#result"); + resultContainer.innerText = `You scored ${quiz.correctAnswers} out of ${quiz.questions.length} correct answers!`; // This value is hardcoded as a placeholder } - -}); \ No newline at end of file + + + const restartButton = document.getElementById("restartButton"); + + restartButton.addEventListener("click", () => { + restartButton.classList.add("hidden"); + quizView.style.display = "block"; + endView.style.display = "none"; + quiz.currentQuestionIndex = 0; + quiz.correctAnswers = 0; + quiz.shuffleQuestions(); + showQuestion(); + quiz.timeRemaining = quizDuration; + timeRemainingContainer.innerText = `${Math.floor(quiz.timeRemaining / 60).toString().padStart(2, "0")}:${(quiz.timeRemaining % 60).toString().padStart(2, "0")}`; +}); + + +interval = setInterval(() =>{ + timeRemainingContainer.innerText = `${Math.floor(quiz.timeRemaining / 60).toString().padStart(2, "0")}:${(quiz.timeRemaining % 60).toString().padStart(2, "0")}`; + quiz.timeRemaining -= 1; + if (quiz.timeRemaining === 0) { + showResults(); + clearInterval(interval); + } +}, 1000); + +}) \ No newline at end of file diff --git a/src/question.js b/src/question.js index 68f6631a..8eb7fdca 100644 --- a/src/question.js +++ b/src/question.js @@ -1,7 +1,17 @@ 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; + } + + shuffleChoices(){ + for(let i = 0; i < this.choices.length; i++){ + const shuffle = Math.floor(Math.random() * (i + 1)); + [this.choices[i], this.choices[shuffle]] = [this.choices[shuffle], this.choices[i]]; + } + } + + } - // 2. shuffleChoices() -} \ No newline at end of file diff --git a/src/quiz.js b/src/quiz.js index d94cfd14..a7b36ba3 100644 --- a/src/quiz.js +++ b/src/quiz.js @@ -1,15 +1,57 @@ 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() { + return this.questions[this.currentQuestionIndex]; +} - // 4. shuffleQuestions() +moveToNextQuestion() { + this.currentQuestionIndex += 1; +} - // 5. checkAnswer(answer) +shuffleQuestions() { + for(let i = 0; i < this.questions.length; i++){ + const shuffle = Math.round(Math.random() * (i + 1)); + [this.questions[i], this.questions[shuffle]] = + [this.questions[shuffle], this.questions[i]]; + } +} + +checkAnswer(answer) { + if (this.questions[this.currentQuestionIndex].answer === answer){ + this.correctAnswers += 1; + return true; + } + return false; +} + +hasEnded() { + if (this.currentQuestionIndex === this.questions.length) { + return true; + } else { + return false; + } +} + +filterQuestionsByDifficulty(difficulty) { + if (difficulty >= 1 && difficulty <= 3) { + this.questions = this.questions.filter((question) => { + return question.difficulty === difficulty + }) + } +} + +averageDifficulty() { + return this.questions.reduce((acc, question) => { + return acc + question.difficulty + }, 0) / this.questions.length +} - // 6. hasEnded() } \ No newline at end of file diff --git a/styles/images/1.jpg b/styles/images/1.jpg new file mode 100644 index 00000000..88963738 Binary files /dev/null and b/styles/images/1.jpg differ diff --git a/styles/images/2.jpg b/styles/images/2.jpg new file mode 100644 index 00000000..fa78091a Binary files /dev/null and b/styles/images/2.jpg differ diff --git a/styles/style.css b/styles/style.css index 8de97e38..6718283a 100644 --- a/styles/style.css +++ b/styles/style.css @@ -6,24 +6,35 @@ } body { + display: grid; + grid-auto-flow: row; + justify-content: center; + align-items: center; + height: 900px; + width: 100%; + background-image: url('/styles/images/1.jpg'); + background-size: cover; + background-repeat: no-repeat; font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; background-color: #f4f4f4; color: #333; line-height: 1.6; - padding: 20px; + padding: 100px; } header { + text-align: center; padding: 0 20px; + text-shadow: 0 2px 5px rgba(0,0,0,0.5); } .container { - height: 540px; - max-width: 600px; + height: 600px; + width: 500px; margin: auto; padding: 20px; background: #fff; - box-shadow: 0 5px 15px rgba(0,0,0,0.1); + box-shadow: 0 5px 15px rgba(0,0,0,0.3); } #quizView, #endView { @@ -49,14 +60,15 @@ button { cursor: pointer; font-size: 16px; width: 160px; + box-shadow: 0 2px 5px rgba(0,0,0,0.1); } .button-primary { - background-color: #4caf50; + background-color: #8ca4b4; } .button-primary:hover { - background-color: #46a049; + background-color:#8ca4b4; } .button-secondary { @@ -73,7 +85,7 @@ button { button:hover { - background-color: #4cae4c; + background-color: #a0eaa0; } ul { @@ -132,7 +144,7 @@ input[type="radio"] { #progressBar { height: 20px; - background-color: #4caf50; + background-color: #537ea9; width: 0%; border-radius: 8px; } @@ -157,7 +169,7 @@ input[type="radio"] { #resultProgressBar { width: 100%; height: 20px; - background-color: #4caf50; + background-color: #537ea9; width: 100%; border-radius: 8px; } diff --git a/tests/quiz.spec.js b/tests/quiz.spec.js index 88a723bd..e9d8b503 100644 --- a/tests/quiz.spec.js +++ b/tests/quiz.spec.js @@ -419,6 +419,8 @@ describe("Quiz", () => { // Check that the averageDifficulty() method returns the correct average when called expect(quiz.averageDifficulty()).toEqual(1.8); }); + + }); });