Skip to content
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
2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -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
110 changes: 101 additions & 9 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ document.addEventListener("DOMContentLoaded", () => {
const questionContainer = document.querySelector("#question");
const choiceContainer = document.querySelector("#choices");
const nextButton = document.querySelector("#nextButton");
const restartButton = document.querySelector("#restartButton"); // create the button restart



// End view elements
const resultContainer = document.querySelector("#result");
Expand Down Expand Up @@ -62,6 +65,7 @@ document.addEventListener("DOMContentLoaded", () => {
let timer;



/************ EVENT LISTENERS ************/

nextButton.addEventListener("click", nextButtonHandler);
Expand All @@ -77,6 +81,8 @@ document.addEventListener("DOMContentLoaded", () => {


function showQuestion() {


// If the quiz has ended, show the results
if (quiz.hasEnded()) {
showResults();
Expand All @@ -92,30 +98,53 @@ document.addEventListener("DOMContentLoaded", () => {
// Shuffle the choices of the current question by calling the method 'shuffleChoices()' on the question object
question.shuffleChoices();


console.log(question)

// YOUR CODE HERE:
//
// 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

const progress = ( (quiz.currentQuestionIndex) / quiz.questions.length) * 100;
progressBar.style.width = `${progress}%`; // 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.innerText = `Question ${quiz.currentQuestionIndex +1} 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) =>{
const radioInput = document.createElement('input');
radioInput.type = "radio"
radioInput.name = "choice"
radioInput.value = choice;


const label = document.createElement("label");
label.innerText = choice;


choiceContainer.appendChild(radioInput);
choiceContainer.appendChild(label);
choiceContainer.appendChild(document.createElement("br"));
// const aDiv = document.createElement('div');
// aDiv.innerHTML =` <input type="radio" name="choice" value="CHOICE TEXT HERE">
// <label>CHOICE TEXT HERE</label>
// <br>`
// choiceContainer.appendChild(aDiv);
// console.log(choice)



})
// 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:
/*
Expand All @@ -134,24 +163,38 @@ document.addEventListener("DOMContentLoaded", () => {

function nextButtonHandler () {
let selectedAnswer; // A variable to store the selected answer value
// Re start the quiz
if (restartButton) {
restartButton.addEventListener("click", restartQuiz);
}




// YOUR CODE HERE:
//
// 1. Get all the choice elements. You can use the `document.querySelectorAll()` method.

const choices = document.querySelectorAll('input[name="choice"]');

// 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.

choices.forEach((choice) => {
if (choice.checked){
selectedAnswer = choice.value;
}
})

// 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.
// Move to the next question by calling the quiz method `moveToNextQuestion()`.
// Show the next question by calling the function `showQuestion()`.
if(selectedAnswer){
quiz.checkAnswer(selectedAnswer)
quiz.moveToNextQuestion();
showQuestion();
}
}


Expand All @@ -168,7 +211,56 @@ 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
resultContainer.innerText = `You scored ${quiz.correctAnswers} out of ${quiz.questions.length} correct answers!`; // This value is hardcoded as a placeholder
clearInterval(timer);
}

function startTimer(){

timer = setInterval(() =>{
quiz.timeRemaining --;
console.log(quiz.timeRemaining);

const minutes = Math.floor(quiz.timeRemaining / 60).toString().padStart(2, "0");

const seconds = (quiz.timeRemaining % 60).toString().padStart(2, "0");
timeRemainingContainer.innerText = `${minutes}:${seconds}`;

if(quiz.timeRemaining <= 0){
clearInterval(timer)
showResults();
}


}, 1000)


}

function restartQuiz() {
// Reset quiz logic
quiz.currentQuestionIndex = 0;
quiz.correctAnswers = 0;
quiz.timeRemaining = quiz.timeLimit; // back to inicial value

// Show the quiz and hide results
endView.style.display = "none";
quizView.style.display = "block";

// 3) Update the timer
const minutes = Math.floor(quiz.timeRemaining / 60).toString().padStart(2, "0");
const seconds = (quiz.timeRemaining % 60).toString().padStart(2, "0");
timeRemainingContainer.innerText = `${minutes}:${seconds}`;

// 4) Restart the timer
startTimer();

// 5) Shows the new question
showQuestion();
}


startTimer();


});
21 changes: 15 additions & 6 deletions src/question.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
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 = Number(difficulty);
}
shuffleChoices() {
for (let i = 0; i < this.choices.length; i++) {
let randomIndex = Math.floor(Math.random() * this.choices.length);
let choiceToMove = this.choices[i];
this.choices.splice(i, 1);
this.choices.splice(randomIndex, 0, choiceToMove);
}
}
}
60 changes: 50 additions & 10 deletions src/quiz.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,55 @@
class Quiz {
// YOUR CODE HERE:
//
// 1. constructor (questions, timeLimit, timeRemaining)
constructor (questions, timeLimit, timeRemaining) {
this.questions = questions;
this.timeLimit = Number(timeLimit);
this.timeRemaining = Number(timeRemaining);
this.correctAnswers = 0;
this.currentQuestionIndex = 0;

// 2. getQuestion()

// 3. moveToNextQuestion()
}
getQuestion(){
return this.questions[this.currentQuestionIndex];
}
moveToNextQuestion() {
this.currentQuestionIndex += 1;
}
shuffleQuestions(){
for (let i = this.questions.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
const temp = this.questions[i];
this.questions[i] = this.questions[j];
this.questions[j] = temp;
}
}

checkAnswer(answer) {
let currentQuestion = this.questions[this.currentQuestionIndex];

// 4. shuffleQuestions()
if (answer === currentQuestion.answer) {
this.correctAnswers += 1;
}
}

hasEnded() {
if (this.currentQuestionIndex < this.questions.length){
return false;
} else if (this.currentQuestionIndex === this.questions.length){
return true;
}
}
filterQuestionsByDifficulty(difficulty){
if(difficulty < 1 || difficulty > 3 || typeof difficulty !== 'number'){
return;
}

return this.questions = this.questions.filter (question => question.difficulty === difficulty)


// 5. checkAnswer(answer)
}
averageDifficulty(){

const totalDificulty = this.questions.reduce((sum, question) => sum + question.difficulty, 0)
return totalDificulty / this.questions.length
}
}

// 6. hasEnded()
}