Skip to content

Commit e460684

Browse files
committed
Added GK Quiz to Games
1 parent 2e8322c commit e460684

3 files changed

Lines changed: 328 additions & 0 deletions

File tree

src/data/content.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import numberblocs from "../assets/numberblocks.png";
1919
import wordleicon from "../assets/games/Wordle/wordlejpg.png";
2020
import flagger from "../assets/games/flag guess/flagger.png";
2121
import Calculator from "../pages/activities/Calculator";
22+
import GKQuiz from "../pages/games/Gk_quiz"
2223
import { DogHttpCode } from "../pages/activities/DogHttpCode";
2324
import { CatHttpCode } from "../pages/activities/CatHttpCode";
2425

@@ -145,4 +146,11 @@ export const games = [
145146
urlTerm: "meme-caption-maker",
146147
element: <MemeCaptionMaker />,
147148
},
149+
{
150+
title: "GK Quiz",
151+
description:"Test your general knowledge with some cool questions",
152+
icon: "https://play-lh.googleusercontent.com/5PyR8hatywCKFQV4wFfsvyK97UrgSn5S1SQILV7zs7rBP5p9VhMEIyjfp_Vdybjk8Qc",
153+
urlTerm:"Gk Quiz",
154+
element:<GKQuiz/>,
155+
}
148156
];

src/pages/games/Gk_quiz.js

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
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+
}

src/styles/pages/games/Gk_quiz.css

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
.gk-quiz-wrapper {
2+
display: flex;
3+
justify-content: center;
4+
padding: 30px 12px;
5+
}
6+
7+
.gk-quiz-container {
8+
width: 100%;
9+
max-width: 650px;
10+
background: #ffffff;
11+
border: 1px solid rgba(15, 23, 42, 0.06);
12+
border-radius: 18px;
13+
box-shadow: 0 14px 35px rgba(15, 23, 42, 0.05);
14+
overflow: hidden;
15+
font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
16+
}
17+
18+
.gk-quiz-header {
19+
background: linear-gradient(120deg, #2563eb 0%, #4f46e5 100%);
20+
padding: 16px 20px 18px;
21+
color: #fff;
22+
}
23+
24+
.gk-quiz-title {
25+
margin: 0;
26+
font-size: 1.3rem;
27+
font-weight: 600;
28+
}
29+
30+
.gk-quiz-subtitle {
31+
margin-top: 4px;
32+
font-size: 0.8rem;
33+
opacity: 0.9;
34+
}
35+
36+
.gk-quiz-body {
37+
padding: 20px;
38+
}
39+
40+
.gk-quiz-progress {
41+
color: #64748b;
42+
margin-bottom: 12px;
43+
font-size: 0.82rem;
44+
}
45+
46+
.gk-quiz-question {
47+
font-size: 1.05rem;
48+
font-weight: 600;
49+
color: #0f172a;
50+
margin-bottom: 16px;
51+
line-height: 1.4;
52+
}
53+
54+
.gk-quiz-options {
55+
display: flex;
56+
flex-direction: column;
57+
gap: 11px;
58+
}
59+
60+
.gk-quiz-option {
61+
text-align: left;
62+
background: #f8fafc;
63+
border: 1px solid rgba(15, 23, 42, 0.03);
64+
padding: 10px 12px;
65+
border-radius: 12px;
66+
cursor: pointer;
67+
transition: 0.12s ease-out;
68+
font-size: 0.93rem;
69+
display: flex;
70+
align-items: center;
71+
gap: 10px;
72+
}
73+
74+
.gk-quiz-option::before {
75+
content: "";
76+
width: 8px;
77+
height: 8px;
78+
border-radius: 9999px;
79+
background: rgba(37, 99, 235, 0.12);
80+
}
81+
82+
.gk-quiz-option:hover:not(:disabled) {
83+
background: #ecf2ff;
84+
border-color: rgba(37, 99, 235, 0.28);
85+
transform: translateY(-1px);
86+
}
87+
88+
.gk-quiz-option:disabled {
89+
cursor: not-allowed;
90+
}
91+
92+
.gk-quiz-option.gk-correct {
93+
background: #dcfce7;
94+
border-color: rgba(34, 197, 94, 0.4);
95+
}
96+
97+
.gk-quiz-option.gk-correct::before {
98+
background: #22c55e;
99+
}
100+
101+
.gk-quiz-option.gk-wrong {
102+
background: #fee2e2;
103+
border-color: rgba(239, 68, 68, 0.4);
104+
}
105+
106+
.gk-quiz-option.gk-wrong::before {
107+
background: #ef4444;
108+
}
109+
110+
.gk-quiz-feedback {
111+
margin-top: 14px;
112+
font-weight: 600;
113+
background: #f8fafc;
114+
border: 1px solid rgba(148, 163, 184, 0.14);
115+
border-radius: 10px;
116+
padding: 9px 11px;
117+
color: #0f172a;
118+
}
119+
120+
.gk-quiz-btn {
121+
margin-top: 18px;
122+
padding: 9px 15px;
123+
border: none;
124+
background: #2563eb;
125+
color: #fff;
126+
border-radius: 9999px;
127+
cursor: pointer;
128+
font-weight: 500;
129+
display: inline-flex;
130+
align-items: center;
131+
gap: 6px;
132+
transition: 0.12s ease-out;
133+
}
134+
135+
.gk-quiz-btn:hover {
136+
background: #1d4ed8;
137+
}
138+
139+
.gk-quiz-result {
140+
text-align: left;
141+
padding: 20px;
142+
}
143+
144+
.gk-quiz-result p {
145+
margin: 5px 0;
146+
}
147+
148+
.gk-quiz-loading {
149+
text-align: center;
150+
margin-top: 40px;
151+
font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
152+
color: #64748b;
153+
}
154+
155+
@media (max-width: 700px) {
156+
.gk-quiz-container {
157+
border-radius: 0;
158+
}
159+
.gk-quiz-body {
160+
padding: 18px 14px 24px;
161+
}
162+
.gk-quiz-question {
163+
font-size: 1rem;
164+
}
165+
.gk-quiz-option {
166+
font-size: 0.9rem;
167+
}
168+
}

0 commit comments

Comments
 (0)