|
| 1 | +import React, { useEffect, useState } from "react"; |
| 2 | +import "../../styles/pages/games/Gk_quiz.css"; |
| 3 | + |
| 4 | +const ALL_QUESTIONS = [ |
| 5 | + { q: "Which is the largest planet in our Solar System?", options: ["Earth", "Jupiter", "Saturn", "Mars"], ans: 1 }, |
| 6 | + { q: "Who wrote the national anthem of India?", options: ["Bankim Chandra Chatterjee", "Rabindranath Tagore", "Sarojini Naidu", "Jawaharlal Nehru"], ans: 1 }, |
| 7 | + { q: "Which is the longest river in the world?", options: ["Nile", "Amazon", "Ganga", "Yangtze"], ans: 0 }, |
| 8 | + { q: "The Red Planet is:", options: ["Mars", "Mercury", "Venus", "Jupiter"], ans: 0 }, |
| 9 | + { q: "Who is known as the Father of Computers?", options: ["Charles Babbage", "Alan Turing", "Tim Berners-Lee", "Bill Gates"], ans: 0 }, |
| 10 | + { q: "Which gas do plants absorb during photosynthesis?", options: ["Oxygen", "Nitrogen", "Carbon dioxide", "Hydrogen"], ans: 2 }, |
| 11 | + { q: "Which country is called the ‘Land of the Rising Sun’?", options: ["India", "Japan", "China", "South Korea"], ans: 1 }, |
| 12 | + { q: "What is the currency of the United Kingdom?", options: ["Euro", "Pound sterling", "Dollar", "Yen"], ans: 1 }, |
| 13 | + { q: "Taj Mahal is located in:", options: ["Delhi", "Agra", "Jaipur", "Varanasi"], ans: 1 }, |
| 14 | + { q: "Which organ purifies blood in the human body?", options: ["Heart", "Lungs", "Kidneys", "Brain"], ans: 2 }, |
| 15 | + { q: "What is the capital of Australia?", options: ["Sydney", "Melbourne", "Canberra", "Perth"], ans: 2 }, |
| 16 | + { q: "Which is the smallest prime number?", options: ["0", "1", "2", "3"], ans: 2 }, |
| 17 | + { q: "Who invented the light bulb?", options: ["Thomas Edison", "Alexander Graham Bell", "Nikola Tesla", "James Watt"], ans: 0 }, |
| 18 | + { q: "Which is the largest ocean on Earth?", options: ["Indian Ocean", "Atlantic Ocean", "Arctic Ocean", "Pacific Ocean"], ans: 3 }, |
| 19 | + { q: "What is the national animal of India?", options: ["Lion", "Tiger", "Elephant", "Peacock"], ans: 1 }, |
| 20 | + { q: "Which festival is known as the Festival of Lights?", options: ["Holi", "Diwali", "Eid", "Baisakhi"], ans: 1 }, |
| 21 | + { q: "How many continents are there in the world?", options: ["5", "6", "7", "8"], ans: 2 }, |
| 22 | + { q: "Which is the highest mountain in the world?", options: ["K2", "Kangchenjunga", "Mount Everest", "Nanda Devi"], ans: 2 }, |
| 23 | + { q: "Who was the first woman Prime Minister of India?", options: ["Indira Gandhi", "Sarojini Naidu", "Pratibha Patil", "Sushma Swaraj"], ans: 0 }, |
| 24 | + { q: "Which is the hardest natural substance?", options: ["Gold", "Iron", "Diamond", "Silver"], ans: 2 } |
| 25 | +]; |
| 26 | + |
| 27 | +function shuffleArray(arr) { |
| 28 | + const copy = [...arr]; |
| 29 | + for (let i = copy.length - 1; i > 0; i--) { |
| 30 | + const j = Math.floor(Math.random() * (i + 1)); |
| 31 | + [copy[i], copy[j]] = [copy[j], copy[i]]; |
| 32 | + } |
| 33 | + return copy; |
| 34 | +} |
| 35 | + |
| 36 | +export default function GKQuiz() { |
| 37 | + const [questions, setQuestions] = useState([]); |
| 38 | + const [current, setCurrent] = useState(0); |
| 39 | + const [correct, setCorrect] = useState(0); |
| 40 | + const [wrong, setWrong] = useState(0); |
| 41 | + const [answered, setAnswered] = useState(false); |
| 42 | + const [chosenIndex, setChosenIndex] = useState(null); |
| 43 | + const [showResult, setShowResult] = useState(false); |
| 44 | + |
| 45 | + useEffect(() => { |
| 46 | + startQuiz(); |
| 47 | + }, []); |
| 48 | + |
| 49 | + function startQuiz() { |
| 50 | + const shuffled = shuffleArray(ALL_QUESTIONS).slice(0, 5); |
| 51 | + setQuestions(shuffled); |
| 52 | + setCurrent(0); |
| 53 | + setCorrect(0); |
| 54 | + setWrong(0); |
| 55 | + setAnswered(false); |
| 56 | + setChosenIndex(null); |
| 57 | + setShowResult(false); |
| 58 | + } |
| 59 | + |
| 60 | + function handleOptionClick(index) { |
| 61 | + if (answered) return; |
| 62 | + const qObj = questions[current]; |
| 63 | + setChosenIndex(index); |
| 64 | + setAnswered(true); |
| 65 | + if (index === qObj.ans) setCorrect((c) => c + 1); |
| 66 | + else setWrong((w) => w + 1); |
| 67 | + } |
| 68 | + |
| 69 | + function handleNext() { |
| 70 | + if (current === questions.length - 1) { |
| 71 | + setShowResult(true); |
| 72 | + return; |
| 73 | + } |
| 74 | + setCurrent((c) => c + 1); |
| 75 | + setAnswered(false); |
| 76 | + setChosenIndex(null); |
| 77 | + } |
| 78 | + |
| 79 | + if (questions.length === 0) return <div className="gk-quiz-loading">Loading quiz…</div>; |
| 80 | + |
| 81 | + if (showResult) { |
| 82 | + const total = questions.length; |
| 83 | + const score = Math.round((correct / total) * 100); |
| 84 | + return ( |
| 85 | + <div className="gk-quiz-wrapper"> |
| 86 | + <div className="gk-quiz-container"> |
| 87 | + <div className="gk-quiz-header"> |
| 88 | + <h2 className="gk-quiz-title">Quiz Completed!</h2> |
| 89 | + <p className="gk-quiz-subtitle">Your performance summary</p> |
| 90 | + </div> |
| 91 | + <div className="gk-quiz-result"> |
| 92 | + <p>Total Questions: {total}</p> |
| 93 | + <p>Correct: {correct}</p> |
| 94 | + <p>Wrong: {wrong}</p> |
| 95 | + <p>Score: {score}%</p> |
| 96 | + <button className="gk-quiz-btn" onClick={startQuiz}>Play Again</button> |
| 97 | + </div> |
| 98 | + </div> |
| 99 | + </div> |
| 100 | + ); |
| 101 | + } |
| 102 | + |
| 103 | + const qObj = questions[current]; |
| 104 | + |
| 105 | + return ( |
| 106 | + <div className="gk-quiz-wrapper"> |
| 107 | + <div className="gk-quiz-container"> |
| 108 | + <div className="gk-quiz-header"> |
| 109 | + <h2 className="gk-quiz-title">General Knowledge Quiz</h2> |
| 110 | + <p className="gk-quiz-subtitle">Answer the questions and check instantly</p> |
| 111 | + </div> |
| 112 | + <div className="gk-quiz-body"> |
| 113 | + <p className="gk-quiz-progress">Question {current + 1} of {questions.length}</p> |
| 114 | + <h3 className="gk-quiz-question">{qObj.q}</h3> |
| 115 | + <div className="gk-quiz-options"> |
| 116 | + {qObj.options.map((opt, idx) => { |
| 117 | + const isCorrect = idx === qObj.ans; |
| 118 | + const isChosen = idx === chosenIndex; |
| 119 | + let extraClass = ""; |
| 120 | + if (answered) { |
| 121 | + if (isCorrect) extraClass = "gk-correct"; |
| 122 | + else if (isChosen && !isCorrect) extraClass = "gk-wrong"; |
| 123 | + } |
| 124 | + return ( |
| 125 | + <button |
| 126 | + key={idx} |
| 127 | + onClick={() => handleOptionClick(idx)} |
| 128 | + className={`gk-quiz-option ${extraClass}`} |
| 129 | + disabled={answered} |
| 130 | + > |
| 131 | + {opt} |
| 132 | + </button> |
| 133 | + ); |
| 134 | + })} |
| 135 | + </div> |
| 136 | + {answered && ( |
| 137 | + <div className="gk-quiz-feedback"> |
| 138 | + {chosenIndex === qObj.ans |
| 139 | + ? <>✅ Correct! The right answer is: {qObj.options[qObj.ans]}</> |
| 140 | + : <>❌ Wrong! The correct answer is: {qObj.options[qObj.ans]}</>} |
| 141 | + </div> |
| 142 | + )} |
| 143 | + {answered && ( |
| 144 | + <button className="gk-quiz-btn" onClick={handleNext}> |
| 145 | + {current === questions.length - 1 ? "Show Result" : "Next"} |
| 146 | + </button> |
| 147 | + )} |
| 148 | + </div> |
| 149 | + </div> |
| 150 | + </div> |
| 151 | + ); |
| 152 | +} |
0 commit comments