40 |
41 | );
42 | };
43 |
44 | export default Quiz;
--------------------------------------------------------------------------------
/src/logo.svg:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/src/context/dataContext.js:
--------------------------------------------------------------------------------
1 | import { createContext, useState, useEffect } from "react";
2 |
3 | const DataContext = createContext({});
4 |
5 | export const DataProvider = ({children}) => {
6 | // All Quizs, Current Question, Index of Current Question, Answer, Selected Answer, Total Marks
7 | const [quizs, setQuizs] = useState([]);
8 | const [question, setQuesion] = useState({});
9 | const [questionIndex, setQuestionIndex] = useState(0);
10 | const [correctAnswer, setCorrectAnswer] = useState('');
11 | const [selectedAnswer, setSelectedAnswer] = useState('');
12 | const [marks, setMarks] = useState(0);
13 |
14 | // Display Controlling States
15 | const [showStart, setShowStart] = useState(true);
16 | const [showQuiz, setShowQuiz] = useState(false);
17 | const [showResult, setShowResult] = useState(false);
18 |
19 | // Load JSON Data
20 | useEffect(() => {
21 | fetch('quiz.json')
22 | .then(res => res.json())
23 | .then(data => setQuizs(data))
24 | }, []);
25 |
26 | // Set a Single Question
27 | useEffect(() => {
28 | if (quizs.length > questionIndex) {
29 | setQuesion(quizs[questionIndex]);
30 | }
31 | }, [quizs, questionIndex])
32 |
33 | // Start Quiz
34 | const startQuiz = () => {
35 | setShowStart(false);
36 | setShowQuiz(true);
37 | }
38 |
39 | // Check Answer
40 | const checkAnswer = (event, selected) => {
41 | if (!selectedAnswer) {
42 | setCorrectAnswer(question.answer);
43 | setSelectedAnswer(selected);
44 |
45 | if (selected === question.answer) {
46 | event.target.classList.add('bg-success');
47 | setMarks(marks + 5);
48 | } else {
49 | event.target.classList.add('bg-danger');
50 | }
51 | }
52 | }
53 |
54 | // Next Quesion
55 | const nextQuestion = () => {
56 | setCorrectAnswer('');
57 | setSelectedAnswer('');
58 | const wrongBtn = document.querySelector('button.bg-danger');
59 | wrongBtn?.classList.remove('bg-danger');
60 | const rightBtn = document.querySelector('button.bg-success');
61 | rightBtn?.classList.remove('bg-success');
62 | setQuestionIndex(questionIndex + 1);
63 | }
64 |
65 | // Show Result
66 | const showTheResult = () => {
67 | setShowResult(true);
68 | setShowStart(false);
69 | setShowQuiz(false);
70 | }
71 |
72 | // Start Over
73 | const startOver = () => {
74 | setShowStart(false);
75 | setShowResult(false);
76 | setShowQuiz(true);
77 | setCorrectAnswer('');
78 | setSelectedAnswer('');
79 | setQuestionIndex(0);
80 | setMarks(0);
81 | const wrongBtn = document.querySelector('button.bg-danger');
82 | wrongBtn?.classList.remove('bg-danger');
83 | const rightBtn = document.querySelector('button.bg-success');
84 | rightBtn?.classList.remove('bg-success');
85 | }
86 | return (
87 |
92 | {children}
93 |
94 | );
95 | }
96 |
97 | export default DataContext;
98 |
99 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Getting Started with Create React App
2 |
3 | This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app).
4 |
5 | ## Available Scripts
6 |
7 | In the project directory, you can run:
8 |
9 | ### `npm start`
10 |
11 | Runs the app in the development mode.\
12 | Open [http://localhost:3000](http://localhost:3000) to view it in your browser.
13 |
14 | The page will reload when you make changes.\
15 | You may also see any lint errors in the console.
16 |
17 | ### `npm test`
18 |
19 | Launches the test runner in the interactive watch mode.\
20 | See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information.
21 |
22 | ### `npm run build`
23 |
24 | Builds the app for production to the `build` folder.\
25 | It correctly bundles React in production mode and optimizes the build for the best performance.
26 |
27 | The build is minified and the filenames include the hashes.\
28 | Your app is ready to be deployed!
29 |
30 | See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information.
31 |
32 | ### `npm run eject`
33 |
34 | **Note: this is a one-way operation. Once you `eject`, you can't go back!**
35 |
36 | If you aren't satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project.
37 |
38 | Instead, it will copy all the configuration files and the transitive dependencies (webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you're on your own.
39 |
40 | You don't have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn't feel obligated to use this feature. However we understand that this tool wouldn't be useful if you couldn't customize it when you are ready for it.
41 |
42 | ## Learn More
43 |
44 | You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started).
45 |
46 | To learn React, check out the [React documentation](https://reactjs.org/).
47 |
48 | ### Code Splitting
49 |
50 | This section has moved here: [https://facebook.github.io/create-react-app/docs/code-splitting](https://facebook.github.io/create-react-app/docs/code-splitting)
51 |
52 | ### Analyzing the Bundle Size
53 |
54 | This section has moved here: [https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size](https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size)
55 |
56 | ### Making a Progressive Web App
57 |
58 | This section has moved here: [https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app](https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app)
59 |
60 | ### Advanced Configuration
61 |
62 | This section has moved here: [https://facebook.github.io/create-react-app/docs/advanced-configuration](https://facebook.github.io/create-react-app/docs/advanced-configuration)
63 |
64 | ### Deployment
65 |
66 | This section has moved here: [https://facebook.github.io/create-react-app/docs/deployment](https://facebook.github.io/create-react-app/docs/deployment)
67 |
68 | ### `npm run build` fails to minify
69 |
70 | This section has moved here: [https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify](https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify)
71 |
--------------------------------------------------------------------------------
/public/quiz.json:
--------------------------------------------------------------------------------
1 | [
2 | {
3 | "id": "01",
4 | "question" : "Which of the following is the correct name of React.js?",
5 | "options": [
6 | "React",
7 | "React.js",
8 | "ReactJS",
9 | "All of the above"
10 | ],
11 | "answer": "All of the above"
12 | },
13 | {
14 | "id": "02",
15 | "question" : "Which of the following are the advantages of React.js?",
16 | "options": [
17 | "React.js can increase the application's performance with Virtual DOM.",
18 | "React.js is easy to integrate with other frameworks such as Angular, BackboneJS since it is only a view library.",
19 | "React.js can render both on client and server side.",
20 | "All of the above"
21 | ],
22 | "answer": "All of the above"
23 | },
24 | {
25 | "id": "03",
26 | "question" : "Which of the following is not a disadvantage of React.js?",
27 | "options": [
28 | "React.js has only a view layer. We have put your code for Ajax requests, events and so on.",
29 | "The library of React.js is pretty large.",
30 | "The JSX in React.js makes code easy to read and write.",
31 | "The learning curve can be steep in React.js."
32 | ],
33 | "answer": "The JSX in React.js makes code easy to read and write."
34 | },
35 | {
36 | "id": "04",
37 | "question" : "Which of the following command is used to install create-react-app?",
38 | "options": [
39 | "npm install -g create-react-app",
40 | "npx create-react-app my-app",
41 | "npm install create-react-app",
42 | "npm install -f create-react-app"
43 | ],
44 | "answer": "npx create-react-app my-app"
45 | },
46 | {
47 | "id": "05",
48 | "question" : "What of the following is used in React.js to increase performance?",
49 | "options": [
50 | "Original DOM",
51 | "Virtual DOM",
52 | "Both A and B.",
53 | "None of the above."
54 | ],
55 | "answer": "Virtual DOM"
56 | },
57 | {
58 | "id": "06",
59 | "question" : "What is the default port where webpack-server runs?",
60 | "options": [
61 | "3000",
62 | "8080",
63 | "3030",
64 | "6060"
65 | ],
66 | "answer": "3000"
67 | },
68 | {
69 | "id": "07",
70 | "question" : "How many numbers of elements a valid react component can return?",
71 | "options": [
72 | "1",
73 | "2",
74 | "3",
75 | "Unlimited"
76 | ],
77 | "answer": "1"
78 | },
79 | {
80 | "id": "08",
81 | "question" : "What is the declarative way to render a dynamic list of components based on values in an array?",
82 | "options": [
83 | "Using the reduce array method",
84 | "Using the component",
85 | "Using the Array.map() method",
86 | "With a for/while loop"
87 | ],
88 | "answer": "Using the Array.map() method"
89 | },
90 | {
91 | "id": "09",
92 | "question" : "What is a state in React?",
93 | "options": [
94 | "A permanent storage.",
95 | "Internal storage of the component.",
96 | "External storage of the component.",
97 | "None of the above."
98 | ],
99 | "answer": "Internal storage of the component."
100 | },
101 | {
102 | "id": "10",
103 | "question" : "What are the two ways to handle data in React?",
104 | "options": [
105 | "State & Props",
106 | "Services & Components",
107 | "State & Services",
108 | "State & Component"
109 | ],
110 | "answer": "State & Props"
111 | }
112 | ]
--------------------------------------------------------------------------------