├── public ├── robots.txt ├── favicon.ico ├── logo192.png ├── logo512.png ├── manifest.json ├── index.html └── quiz.json ├── src ├── setupTests.js ├── App.test.js ├── reportWebVitals.js ├── App.js ├── index.js ├── App.css ├── components │ ├── Start.js │ ├── Result.js │ └── Quiz.js ├── index.css ├── logo.svg └── context │ └── dataContext.js ├── .gitignore ├── package.json └── README.md /public/robots.txt: -------------------------------------------------------------------------------- 1 | # https://www.robotstxt.org/robotstxt.html 2 | User-agent: * 3 | Disallow: 4 | -------------------------------------------------------------------------------- /public/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/md-kawsar-ali/React-Quiz-App/HEAD/public/favicon.ico -------------------------------------------------------------------------------- /public/logo192.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/md-kawsar-ali/React-Quiz-App/HEAD/public/logo192.png -------------------------------------------------------------------------------- /public/logo512.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/md-kawsar-ali/React-Quiz-App/HEAD/public/logo512.png -------------------------------------------------------------------------------- /src/setupTests.js: -------------------------------------------------------------------------------- 1 | // jest-dom adds custom jest matchers for asserting on DOM nodes. 2 | // allows you to do things like: 3 | // expect(element).toHaveTextContent(/react/i) 4 | // learn more: https://github.com/testing-library/jest-dom 5 | import '@testing-library/jest-dom'; 6 | -------------------------------------------------------------------------------- /src/App.test.js: -------------------------------------------------------------------------------- 1 | import { render, screen } from '@testing-library/react'; 2 | import App from './App'; 3 | 4 | test('renders learn react link', () => { 5 | render(); 6 | const linkElement = screen.getByText(/learn react/i); 7 | expect(linkElement).toBeInTheDocument(); 8 | }); 9 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # See https://help.github.com/articles/ignoring-files/ for more about ignoring files. 2 | 3 | # dependencies 4 | /node_modules 5 | /.pnp 6 | .pnp.js 7 | 8 | # testing 9 | /coverage 10 | 11 | # production 12 | /build 13 | 14 | # misc 15 | .DS_Store 16 | .env.local 17 | .env.development.local 18 | .env.test.local 19 | .env.production.local 20 | 21 | npm-debug.log* 22 | yarn-debug.log* 23 | yarn-error.log* 24 | -------------------------------------------------------------------------------- /src/reportWebVitals.js: -------------------------------------------------------------------------------- 1 | const reportWebVitals = onPerfEntry => { 2 | if (onPerfEntry && onPerfEntry instanceof Function) { 3 | import('web-vitals').then(({ getCLS, getFID, getFCP, getLCP, getTTFB }) => { 4 | getCLS(onPerfEntry); 5 | getFID(onPerfEntry); 6 | getFCP(onPerfEntry); 7 | getLCP(onPerfEntry); 8 | getTTFB(onPerfEntry); 9 | }); 10 | } 11 | }; 12 | 13 | export default reportWebVitals; 14 | -------------------------------------------------------------------------------- /src/App.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import Start from './components/Start'; 3 | import Quiz from './components/Quiz'; 4 | import Result from './components/Result'; 5 | import { DataProvider } from './context/dataContext'; 6 | 7 | function App() { 8 | return ( 9 | 10 | {/* Welcome Page */} 11 | 12 | 13 | {/* Quiz Page */} 14 | 15 | 16 | {/* Result Page */} 17 | 18 | 19 | 20 | ); 21 | } 22 | 23 | export default App; 24 | -------------------------------------------------------------------------------- /public/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "short_name": "React App", 3 | "name": "Create React App Sample", 4 | "icons": [ 5 | { 6 | "src": "favicon.ico", 7 | "sizes": "64x64 32x32 24x24 16x16", 8 | "type": "image/x-icon" 9 | }, 10 | { 11 | "src": "logo192.png", 12 | "type": "image/png", 13 | "sizes": "192x192" 14 | }, 15 | { 16 | "src": "logo512.png", 17 | "type": "image/png", 18 | "sizes": "512x512" 19 | } 20 | ], 21 | "start_url": ".", 22 | "display": "standalone", 23 | "theme_color": "#000000", 24 | "background_color": "#ffffff" 25 | } 26 | -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import ReactDOM from 'react-dom/client'; 3 | import reportWebVitals from './reportWebVitals'; 4 | import 'bootstrap/dist/css/bootstrap.min.css'; 5 | import './index.css'; 6 | import App from './App'; 7 | 8 | const root = ReactDOM.createRoot(document.getElementById('root')); 9 | root.render( 10 | 11 | 12 | 13 | ); 14 | 15 | // If you want to start measuring performance in your app, pass a function 16 | // to log results (for example: reportWebVitals(console.log)) 17 | // or send to an analytics endpoint. Learn more: https://bit.ly/CRA-vitals 18 | reportWebVitals(); 19 | -------------------------------------------------------------------------------- /src/App.css: -------------------------------------------------------------------------------- 1 | .App { 2 | text-align: center; 3 | } 4 | 5 | .App-logo { 6 | height: 40vmin; 7 | pointer-events: none; 8 | } 9 | 10 | @media (prefers-reduced-motion: no-preference) { 11 | .App-logo { 12 | animation: App-logo-spin infinite 20s linear; 13 | } 14 | } 15 | 16 | .App-header { 17 | background-color: #282c34; 18 | min-height: 100vh; 19 | display: flex; 20 | flex-direction: column; 21 | align-items: center; 22 | justify-content: center; 23 | font-size: calc(10px + 2vmin); 24 | color: white; 25 | } 26 | 27 | .App-link { 28 | color: #61dafb; 29 | } 30 | 31 | @keyframes App-logo-spin { 32 | from { 33 | transform: rotate(0deg); 34 | } 35 | to { 36 | transform: rotate(360deg); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/components/Start.js: -------------------------------------------------------------------------------- 1 | import React, { useContext } from 'react'; 2 | import DataContext from '../context/dataContext'; 3 | 4 | const Start = () => { 5 | const {startQuiz, showStart} = useContext(DataContext); 6 | return ( 7 |
8 |
9 |
10 |
11 |

Basic React JS Quiz

12 | 13 |
14 |
15 |
16 |
17 | ); 18 | }; 19 | 20 | export default Start; -------------------------------------------------------------------------------- /src/index.css: -------------------------------------------------------------------------------- 1 | body { 2 | margin: 0; 3 | font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 4 | 'Oxygen', 'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 5 | 'Helvetica Neue', sans-serif; 6 | -webkit-font-smoothing: antialiased; 7 | -moz-osx-font-smoothing: grayscale; 8 | letter-spacing: 0.5px; 9 | } 10 | 11 | code { 12 | font-family: source-code-pro, Menlo, Monaco, Consolas, 'Courier New', 13 | monospace; 14 | } 15 | 16 | h1, 17 | h2, 18 | h3 { 19 | font-family: 'Helvetica Neue'; 20 | font-weight: 600; 21 | letter-spacing: 0.5px; 22 | } 23 | 24 | .btn, 25 | .btn-dark, 26 | .btn-check:focus + .btn-dark, 27 | .btn-dark:focus, 28 | .btn-check:focus + .btn, 29 | .btn:focus { 30 | border: none; 31 | box-shadow: none; 32 | } 33 | 34 | .btn-dark:hover { 35 | background: #212529; 36 | } 37 | 38 | .bg-primary, 39 | .btn-light { 40 | letter-spacing: 0.7px; 41 | } 42 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "quiz-app", 3 | "version": "0.1.0", 4 | "private": true, 5 | "dependencies": { 6 | "@testing-library/jest-dom": "^5.16.5", 7 | "@testing-library/react": "^13.3.0", 8 | "@testing-library/user-event": "^13.5.0", 9 | "bootstrap": "^5.1.3", 10 | "react": "^18.0.0", 11 | "react-bootstrap": "^2.3.0", 12 | "react-dom": "^18.0.0", 13 | "react-scripts": "^5.0.1", 14 | "web-vitals": "^2.1.4" 15 | }, 16 | "scripts": { 17 | "start": "react-scripts start", 18 | "build": "react-scripts build", 19 | "test": "react-scripts test", 20 | "eject": "react-scripts eject" 21 | }, 22 | "eslintConfig": { 23 | "extends": [ 24 | "react-app", 25 | "react-app/jest" 26 | ] 27 | }, 28 | "browserslist": { 29 | "production": [ 30 | ">0.2%", 31 | "not dead", 32 | "not op_mini all" 33 | ], 34 | "development": [ 35 | "last 1 chrome version", 36 | "last 1 firefox version", 37 | "last 1 safari version" 38 | ] 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/components/Result.js: -------------------------------------------------------------------------------- 1 | import React, { useContext } from 'react'; 2 | import DataContext from '../context/dataContext'; 3 | 4 | const Result = () => { 5 | const { showResult, quizs, marks, startOver } = useContext(DataContext); 6 | return ( 7 |
8 |
9 |
10 |
11 |
(quizs.length * 5 / 2) ? 'bg-success' : 'bg-danger'}`}> 12 |

{marks > (quizs.length * 5 / 2) ? 'Awesome!' : 'Oops!'}

13 |

Your score is {marks} out of {quizs.length * 5}

14 | 15 | 16 |
17 |
18 |
19 |
20 |
21 | ); 22 | }; 23 | 24 | export default Result; -------------------------------------------------------------------------------- /public/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 15 | 16 | 25 | React Quiz APP 26 | 27 | 28 | 29 | 30 |
31 | 41 | 42 | 43 | -------------------------------------------------------------------------------- /src/components/Quiz.js: -------------------------------------------------------------------------------- 1 | import React, { useContext } from 'react'; 2 | import DataContext from '../context/dataContext'; 3 | 4 | const Quiz = () => { 5 | const { showQuiz, question, quizs, checkAnswer, correctAnswer, 6 | selectedAnswer,questionIndex, nextQuestion, showTheResult } = useContext(DataContext); 7 | 8 | return ( 9 |
10 |
11 |
12 |
13 |
14 |
15 |
{question?.question}
16 |
{quizs.indexOf(question) + 1} / {quizs?.length}
17 |
18 |
19 | { 20 | question?.options?.map((item, index) => ) 27 | } 28 |
29 | 30 | { 31 | (questionIndex + 1) !== quizs.length ? 32 | 33 | : 34 | 35 | } 36 |
37 |
38 |
39 |
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 | ] --------------------------------------------------------------------------------