diff --git a/reactjs-interview-questions/quiz-app/src/App.css b/reactjs-interview-questions/quiz-app/src/App.css index dcc08d9..297fda1 100644 --- a/reactjs-interview-questions/quiz-app/src/App.css +++ b/reactjs-interview-questions/quiz-app/src/App.css @@ -36,7 +36,7 @@ button { /* border: 1px solid grey; */ } -button:hover { +button:hover, .active { background-color: green; color: white; } @@ -71,3 +71,7 @@ button:hover { .results li[data-correct="false"] { color: red; } + +.questionNumber{ + margin: 5px 5px; +} \ No newline at end of file diff --git a/reactjs-interview-questions/quiz-app/src/App.jsx b/reactjs-interview-questions/quiz-app/src/App.jsx index 61ee70b..27b7f33 100644 --- a/reactjs-interview-questions/quiz-app/src/App.jsx +++ b/reactjs-interview-questions/quiz-app/src/App.jsx @@ -1,34 +1,52 @@ -import {useState} from "react"; +import { useState } from "react"; import "./App.css"; import questions from "./constants/questions.json"; import Question from "./components/question"; import Result from "./components/result"; +import QuestionNumber from "./components/QuestionNumber"; function App() { const [currentQuestion, setCurrentQuestion] = useState(0); const [userAnswers, setUserAnswers] = useState([]); + const [activeQuestion, setActiveQuestion] = useState(0); // Keep all of the logic in App.jsx - const handleNextQuestion = (isCorrect) => { - setCurrentQuestion(currentQuestion + 1); - setUserAnswers([...userAnswers, isCorrect]); + const handleNextQuestion = (newQues) => { + setCurrentQuestion(parseInt(currentQuestion) + 1); + setActiveQuestion(parseInt(currentQuestion) + 1); + setUserAnswers([...userAnswers, newQues]); }; const resetQuiz = () => { setCurrentQuestion(0); setUserAnswers([]); + setActiveQuestion(0); }; + const onClickQuestion = (index) => { + setActiveQuestion(parseInt(index)); + setCurrentQuestion(parseInt(index)); + }; return (

World Quiz

+ {/* Questions Marking */} + {currentQuestion < questions.length && ( + + )} + {/* Questions Component */} {currentQuestion < questions.length && ( )} diff --git a/reactjs-interview-questions/quiz-app/src/components/QuestionNumber.jsx b/reactjs-interview-questions/quiz-app/src/components/QuestionNumber.jsx new file mode 100644 index 0000000..2c10af1 --- /dev/null +++ b/reactjs-interview-questions/quiz-app/src/components/QuestionNumber.jsx @@ -0,0 +1,22 @@ +import React from "react"; + +const QuestionNumber = ({ + questions, + activeQuestion, + onClickQuestion = () => {}, +}) => { + return questions.map((q, index) => ( + + )); +}; + +export default QuestionNumber; diff --git a/reactjs-interview-questions/quiz-app/src/components/question.jsx b/reactjs-interview-questions/quiz-app/src/components/question.jsx index 71f57cd..71ee147 100644 --- a/reactjs-interview-questions/quiz-app/src/components/question.jsx +++ b/reactjs-interview-questions/quiz-app/src/components/question.jsx @@ -1,6 +1,6 @@ /* eslint-disable react/prop-types */ -const Question = ({question, onAnswerClick = () => {}}) => { +const Question = ({ question, onAnswerClick = () => {} }) => { return (

{question.question}

@@ -8,7 +8,15 @@ const Question = ({question, onAnswerClick = () => {}}) => { {question.answerOptions.map((option) => { return (
  • -
  • diff --git a/reactjs-interview-questions/quiz-app/src/components/result.jsx b/reactjs-interview-questions/quiz-app/src/components/result.jsx index c6dbf63..9b11f2c 100644 --- a/reactjs-interview-questions/quiz-app/src/components/result.jsx +++ b/reactjs-interview-questions/quiz-app/src/components/result.jsx @@ -1,7 +1,9 @@ /* eslint-disable react/prop-types */ -const Result = ({userAnswers, questions, resetQuiz = () => {}}) => { - const correctAnswers = userAnswers.filter((answer) => answer).length; +const Result = ({ userAnswers, questions, resetQuiz = () => {} }) => { + const correctAnswers = userAnswers.filter( + (answer) => answer.isCorrect + ).length; return (
    @@ -13,10 +15,15 @@ const Result = ({userAnswers, questions, resetQuiz = () => {}}) => {
      {questions.map((question, index) => { return ( -
    • +
    • q.question === question.question) + .map((m) => m.isCorrect)} + > Q{index + 1}. {question.question} - {userAnswers[index] + {userAnswers[index].isCorrect ? "" : `- ${ question.answerOptions.find((ans) => ans.isCorrect).text