Skip to content

Solved Mini Project #221

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
4 changes: 2 additions & 2 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ <h1>JavaScript Quiz</h1>
<div id="progressBar"></div>
</div>
<div id="questionCount"></div>

<div class="content">
<div id="question"></div>
<ul id="choices"></ul>
Expand All @@ -39,7 +39,7 @@ <h1>JavaScript Quiz</h1>
<div id="result"></div>
</div>
<!-- The below 'Restart Quiz' button is commented out because it is not used initially -->
<!-- <button id="restartButton" class="button-secondary">Restart Quiz</button> -->
<button id="restartButton" class="button-secondary">Restart Quiz</button>
</div>
</div>

Expand Down
74 changes: 63 additions & 11 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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+= `<input type="radio" name="choice" value="${choice}">
<label>${choice}</label>
<br>`;

// 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:
/*
<input type="radio" name="choice" value="CHOICE TEXT HERE">
<label>CHOICE TEXT HERE</label>
<br>

*/
// 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
Expand All @@ -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.
Expand All @@ -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
}

});


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);

})
20 changes: 15 additions & 5 deletions src/question.js
Original file line number Diff line number Diff line change
@@ -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()
}
58 changes: 50 additions & 8 deletions src/quiz.js
Original file line number Diff line number Diff line change
@@ -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()
}
Binary file added styles/images/1.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added styles/images/2.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
30 changes: 21 additions & 9 deletions styles/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -73,7 +85,7 @@ button {


button:hover {
background-color: #4cae4c;
background-color: #a0eaa0;
}

ul {
Expand Down Expand Up @@ -132,7 +144,7 @@ input[type="radio"] {

#progressBar {
height: 20px;
background-color: #4caf50;
background-color: #537ea9;
width: 0%;
border-radius: 8px;
}
Expand All @@ -157,7 +169,7 @@ input[type="radio"] {
#resultProgressBar {
width: 100%;
height: 20px;
background-color: #4caf50;
background-color: #537ea9;
width: 100%;
border-radius: 8px;
}
Expand Down
2 changes: 2 additions & 0 deletions tests/quiz.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,8 @@ describe("Quiz", () => {
// Check that the averageDifficulty() method returns the correct average when called
expect(quiz.averageDifficulty()).toEqual(1.8);
});


});
});

Expand Down