diff --git a/src/components/Answer/Answer.js b/src/components/Answer/Answer.js index aed2bf9..7eda1dd 100644 --- a/src/components/Answer/Answer.js +++ b/src/components/Answer/Answer.js @@ -1,14 +1,15 @@ export default class Answer { - constructor(timeOfAnswer, Question, answer, lifelineUsed, changed) { + constructor(index, timeOfAnswer, question, answer, lifelineUsed, changed) { this.timeOfAnswer = timeOfAnswer; - this.Question = Question; + this.question = question; this.lifelineUsed = lifelineUsed; this.answer = answer; this.changed = changed; + this.index = index; } checkIfCorrect() { - return this.answer.toUpperCase() === this.Question.correct.toUpperCase(); + return this.answer.toUpperCase() === this.question.correct.toUpperCase(); } getScore() { @@ -16,7 +17,7 @@ export default class Answer { if (this.checkIfCorrect()) { score = 1; if (this.changed) { - score /= score / 2; + score /= 2; } } return score; diff --git a/src/views/QuizView/quizView.css b/src/views/QuizView/quizView.css index 28eef59..37c6ef4 100644 --- a/src/views/QuizView/quizView.css +++ b/src/views/QuizView/quizView.css @@ -50,8 +50,18 @@ text-align: center; font-weight: bold; } - #quizView #question-numbers #current-question { + line-height: 5vmin; + width: 5vmin; + border-radius: 50%; + padding: 2vmin; + border: 0.3vmin solid #000; + color: white; + text-align: center; + background-color: #000; +} + +#quizView #question-numbers .answered { line-height: 4vmin; width: 4vmin; border-radius: 50%; @@ -124,6 +134,11 @@ left: 2%; } +.wrapper.selected .answer { + background-color: #3e505b; + color: white; +} + @media only screen and (min-device-width: 280px) and (max-device-width: 660px) { .quizViewMenuButton { width: min-content; diff --git a/src/views/QuizView/quizView.js b/src/views/QuizView/quizView.js index bc13519..df5deca 100644 --- a/src/views/QuizView/quizView.js +++ b/src/views/QuizView/quizView.js @@ -1,5 +1,5 @@ import { QuizSettings } from '../quiz-settings/quiz-settings'; -import { createTimer, startTimer, stopTimer } from '../../components/timer/quiz-timer'; +import { createTimer, startTimer } from '../../components/timer/quiz-timer'; import { getRandomQuizQuestions } from '../../model/randomizer'; import { renderChoiceModal } from '../confirmChoiceModal/confirmChoiceModal'; import Button from '../../components/Button/Button'; @@ -19,6 +19,7 @@ export async function renderQuizView() { current = 0; userAnswers = []; document.querySelector('#app').append(createLayout()); + questions.forEach((q, idx) => userAnswers.push(new Answer(idx, 0, q, '', false, false))); startTimer(); renderQuizData(); } @@ -26,6 +27,9 @@ export async function renderQuizView() { function createQuestionNumbers() { const circles = document.createElement('div'); circles.setAttribute('id', 'question-numbers'); + circles.onclick = function click(e) { + selectQuestion(parseFloat(e.target.innerText)); + }; for (let i = 0; i < QuizSettings.questionsNum; i++) { const number = document.createElement('div'); number.innerText = i + 1; @@ -112,17 +116,21 @@ function renderQuizData() { current = current === QuizSettings.questionsNum ? current - 1 : current; const numbers = document.getElementById('question-numbers').children; numbers.item(current).setAttribute('id', 'current-question'); - const image = document.querySelector('#quizView > img'); - image.src = questions[current].imageUrl; + image.src = userAnswers[current].question.imageUrl; const question = document.getElementById('question-text'); - question.innerText = questions[current].question.toUpperCase(); + question.innerText = userAnswers[current].question.question.toUpperCase(); const answersContainer = document.getElementById('answers'); - const answers = questions[current].getAnswers(); + const answers = userAnswers[current].question.getAnswers(); + for (let i = 0; i < answers.length; i++) { - answersContainer.appendChild(Button(answers[i], 'answer', false, 'click', nextQuestion)); + const answerBtn = Button(answers[i], 'answer', false, 'click', nextQuestion); + if (answers[i].toUpperCase() === userAnswers[current].answer) { + answerBtn.classList.add('selected'); + } + answersContainer.appendChild(answerBtn); } startTime = getTime(); } @@ -135,28 +143,30 @@ function getTime() { } function saveAnswer(answer) { - let change = false; endTime = getTime(); const relativeTime = endTime - startTime; - if (userAnswers[current] != undefined && userAnswers[current].answer != '') { - if (answer === '') { - return; - } - change = true; - } - const ans = new Answer(relativeTime, questions[current], answer, false, change); - userAnswers[current] = ans; + + const currentAnswer = userAnswers.find((ans) => current === ans.index); + currentAnswer.timeOfAnswer = relativeTime; + currentAnswer.changed = currentAnswer.answer && currentAnswer.answer !== answer; + currentAnswer.answer = answer; +} + +function addAnsweredClass() { + const currentQuestionNumber = document.getElementById('current-question'); + currentQuestionNumber.setAttribute('class', 'answered'); } function nextQuestion(e) { - let buttonValue = e.target.innerText; + const buttonValue = e.target.innerText; saveAnswer(buttonValue); - const currentQuestionNumber = document.getElementById('current-question'); - currentQuestionNumber.setAttribute('id', ''); + if (userAnswers[current].answer) { + addAnsweredClass(); + } - const answersContainer = document.getElementById('answers'); - answersContainer.innerHTML = ''; + clearCurrentQuestionId(); + clearAnswerContainer(); current++; lifeline = false; @@ -164,13 +174,36 @@ function nextQuestion(e) { } function previousQuestion() { + if (userAnswers[current].answer) { + addAnsweredClass(); + } + + clearCurrentQuestionId(); + clearAnswerContainer(); + + current--; + renderQuizData(); +} + +function clearCurrentQuestionId() { const currentQuestionNumber = document.getElementById('current-question'); - currentQuestionNumber.setAttribute('id', ''); + currentQuestionNumber.removeAttribute('id'); +} +function clearAnswerContainer() { const answersContainer = document.getElementById('answers'); answersContainer.innerHTML = ''; +} - current--; +function selectQuestion(selectedNumber) { + if (userAnswers[current].answer) { + addAnsweredClass(); + } + + clearCurrentQuestionId(); + clearAnswerContainer(); + + current = selectedNumber - 1; renderQuizData(); } @@ -185,8 +218,8 @@ function useLifeline() { let n; while (removed < 2) { n = Math.floor(Math.random() * answers.length); - if (questions[current].correct.toUpperCase() != answers[n].children[0].innerText) { - let index = questions[current].incorrectAnswers + if (questions[current].correct.toUpperCase() !== answers[n].children[0].innerText) { + const index = questions[current].incorrectAnswers .map((ans) => ans.toUpperCase()) .indexOf(answers[n].children[0].innerText); questions[current].incorrectAnswers.splice(index, 1);