Quize App use React js - TechXplore

Thursday, 21 December 2023

Quize App use React js

 QuizeApp 

I have created the simple quiz app using frontend development React JS and added the style using CSS.





quizapp.js file:

import React from "react";

import "./quizeapp.css";


class QuizApp extends React.Component {

  constructor(props) {

    super(props);

    this.state = {

      questions: [

        {

          question: 'What is the capital of France?',

          options: ['Paris', 'Berlin', 'Madrid', 'Rome'],

          correctAnswer: 'Paris'

        },

        {

          question: 'Which planet is known as the Red Planet?',

          options: ['Mars', 'Jupiter', 'Venus', 'Saturn'],

          correctAnswer: 'Mars'

        },

        {

          question: 'What is the largest mammal?',

          options: ['Elephant', 'Blue Whale', 'Giraffe', 'Hippopotamus'],

          correctAnswer: 'Blue Whale'

        }

      ],

      currentQuestion: 0,

      userAnswers: [],

      score: 0

    };

  }


  handleOptionClick = (selectedOption) => {

    const { currentQuestion, questions, userAnswers, score } = this.state;


    const isCorrect = selectedOption === questions[currentQuestion].correctAnswer;


    this.setState({

      userAnswers: [...userAnswers, { question: currentQuestion, answer: selectedOption, isCorrect }],

      score: isCorrect ? score + 1 : score

    });


    // Move to the next question

    this.setState((prevState) => ({ currentQuestion: prevState.currentQuestion + 1 }));

  };


//hanldeBackButton

handleBackButtonClick = () => {

  // Go back to the previous question

  this.setState((prevState) => ({ currentQuestion: prevState.currentQuestion - 1 }));

};


//handleHoeButton

handleHomeButtonClick = () => {

  // Go back to the home page

  this.setState({ currentQuestion: 0, userAnswers: [], score: 0 });

};




  render() {

    const { questions, currentQuestion, userAnswers, score } = this.state;

    const totalQuestions = questions.length;

    const isLastQuestion = currentQuestion === totalQuestions;


    return (

      <div className="question-container">

        

        {isLastQuestion ? (

          <div className="result-container">

            <h2>Your Score: {score}</h2>

             

            <ul>

              {userAnswers.map((answer, index) => (

                <li key={index} className={answer.isCorrect ? 'correct' : 'incorrect'}>

                  <p>

                    Question {answer.question + 1}:

                    {answer.isCorrect ? ' Correct' : ' Incorrect'}

                  </p>

                  <p>Your Answer: {answer.answer}</p>

                  <p>Correct Answer: {questions[answer.question].correctAnswer}</p>

                </li>

                

              ))}

            </ul>

            <button className="Back-Score" onClick={this.handleHomeButtonClick}>Back</button>

                  <p>

                  Page {currentQuestion + 1} of {totalQuestions}

                </p>

          </div>

        ) : (

      <div className="quesFlex"> 

            <div className="question">

              <h2>Question {currentQuestion + 1}</h2>

              <p>{questions[currentQuestion].question}</p>

            


            <div className="answer">

              {questions[currentQuestion].options.map((option, index) => (

                <button key={index} className="Answer-button" onClick={() => this.handleOptionClick(option)}>

                  {option}

                </button>

              ))}

  <button className="Back"onClick={this.handleBackButtonClick}>Back</button>

            </div>

               <p className="Page-Number">Page Number {currentQuestion + 1} of {totalQuestions}</p>

            </div>


           

          </div>

        )}

      </div>

    );

  }

}


export default QuizApp;

 quizapp.css

body {
  font-family: 'Arial', sans-serif;
  margin: 0;
  padding: 0;
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-color: #5257f0;
}
.question-container{
  background-color: #0b0281;
  border-radius: 8px;
  display: flex;
 justify-content: space-between;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.8);
  padding: 20px;
  width: 400px;
  width: 100%;
  color: white;
}
.quesFlex{
  display: flex;
  flex-direction: row;
}
.Back{
  padding:  8px 20px;
  margin-top: 150px;
  border: none;
  border-radius: 8px;
  background-color: #1a0bf6;
  font-size: 12px;
  font-weight: bold;
  color: white;
  cursor: pointer;

}

.answer{
display: flex;
flex-direction: column;
margin-left: 40px;
}

.Answer-button{
  margin: 5px;
  margin-top: 7px;
  padding: 15px 100px;
  border: none;
  border-radius: 10px;
  background-color: white;
  font-size: 13px;
  font-weight: bold;
  cursor: pointer;
}
.Answer-button:hover{
  background-color:rgb(111, 141, 247);
  color: black;
}
.Back-Score{
  padding:  8px 20px;
 margin: 5px;
  border: none;
  border-radius: 8px;
  background-color: #1a0bf6;
  font-size: 12px;
  font-weight: bold;
  color: white;
  cursor: pointer;
}
.Page-Number{
  margin-left: 240px;
  margin-top:30px;
}
h2{
  color: white;
}
.result-container {
  text-align: center;
  margin-left: 40px;
}

.result-container ul {
  padding: 0;
}

.result-container li {
  background-color: #fff;
  border-radius: 8px;
  color: black;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
  margin: 10px 0;
  padding: 20px;
  font-weight: bold;
  list-style: none;
}

.correct {
  border: 2px solid #4caf50;
 

}

.incorrect {
  border: 2px solid #e74c3c;
 
}

.result-container li p {
  margin: 5px 0;
}



.result-container li .correct span {
  color: #4caf50;
}

.result-container li .incorrect span {
  color: #e74c3c;
}

app.js

import QuizApp from "./quizeapp";


function App(){ 

  return (

   <QuizApp/>

  );

}


export default App;

OutPut:









No comments:

Post a Comment