Update components/MathQuizApp.tsx
Browse files- components/MathQuizApp.tsx +177 -58
components/MathQuizApp.tsx
CHANGED
@@ -1,12 +1,13 @@
|
|
1 |
'use client';
|
2 |
|
3 |
import { useState } from 'react';
|
4 |
-
import { Check, X } from 'lucide-react';
|
5 |
|
|
|
6 |
const operations = ['+', '-', '*', '/'];
|
7 |
const difficulties = ['easy', 'medium', 'hard'];
|
8 |
|
9 |
-
const generateQuestion = (difficulty
|
10 |
const operation = operations[Math.floor(Math.random() * operations.length)];
|
11 |
let num1 = 0;
|
12 |
let num2 = 0;
|
@@ -34,6 +35,103 @@ const generateQuestion = (difficulty: string) => {
|
|
34 |
};
|
35 |
};
|
36 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
const MathQuizApp = () => {
|
38 |
const [question, setQuestion] = useState(generateQuestion('easy'));
|
39 |
const [userAnswer, setUserAnswer] = useState('');
|
@@ -41,8 +139,9 @@ const MathQuizApp = () => {
|
|
41 |
const [score, setScore] = useState(0);
|
42 |
const [wrongAnswers, setWrongAnswers] = useState(0);
|
43 |
const [difficulty, setDifficulty] = useState('easy');
|
|
|
44 |
|
45 |
-
const handleSubmit = (e
|
46 |
e.preventDefault();
|
47 |
if (Number(userAnswer) === question.answer) {
|
48 |
setResult('Correct!');
|
@@ -55,7 +154,7 @@ const MathQuizApp = () => {
|
|
55 |
setQuestion(generateQuestion(difficulty));
|
56 |
};
|
57 |
|
58 |
-
const handleDifficultyChange = (e
|
59 |
setDifficulty(e.target.value);
|
60 |
setQuestion(generateQuestion(e.target.value));
|
61 |
setScore(0);
|
@@ -63,64 +162,84 @@ const MathQuizApp = () => {
|
|
63 |
};
|
64 |
|
65 |
return (
|
66 |
-
<div className="bg-gray-50 min-h-screen flex items-center justify-center">
|
67 |
-
<div className="max-w-4xl mx-auto
|
68 |
-
<
|
69 |
-
|
70 |
-
<
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
117 |
)}
|
118 |
-
</
|
|
|
|
|
|
|
|
|
|
|
|
|
119 |
)}
|
120 |
-
</
|
121 |
</div>
|
122 |
</div>
|
123 |
);
|
124 |
};
|
125 |
|
126 |
-
export default MathQuizApp;
|
|
|
1 |
'use client';
|
2 |
|
3 |
import { useState } from 'react';
|
4 |
+
import { Check, X, Calculator } from 'lucide-react';
|
5 |
|
6 |
+
// Import all the calculator hooks and logic from the first component
|
7 |
const operations = ['+', '-', '*', '/'];
|
8 |
const difficulties = ['easy', 'medium', 'hard'];
|
9 |
|
10 |
+
const generateQuestion = (difficulty) => {
|
11 |
const operation = operations[Math.floor(Math.random() * operations.length)];
|
12 |
let num1 = 0;
|
13 |
let num2 = 0;
|
|
|
35 |
};
|
36 |
};
|
37 |
|
38 |
+
const CalculatorComponent = ({ onClose }) => {
|
39 |
+
const [display, setDisplay] = useState('0');
|
40 |
+
const [memory, setMemory] = useState(null);
|
41 |
+
const [firstOperand, setFirstOperand] = useState(null);
|
42 |
+
const [operator, setOperator] = useState(null);
|
43 |
+
const [waitingForSecondOperand, setWaitingForSecondOperand] = useState(false);
|
44 |
+
|
45 |
+
const inputDigit = (digit) => {
|
46 |
+
if (waitingForSecondOperand) {
|
47 |
+
setDisplay(digit);
|
48 |
+
setWaitingForSecondOperand(false);
|
49 |
+
} else {
|
50 |
+
setDisplay(display === '0' ? digit : display + digit);
|
51 |
+
}
|
52 |
+
};
|
53 |
+
|
54 |
+
const inputDecimal = () => {
|
55 |
+
if (waitingForSecondOperand) {
|
56 |
+
setDisplay('0.');
|
57 |
+
setWaitingForSecondOperand(false);
|
58 |
+
return;
|
59 |
+
}
|
60 |
+
if (!display.includes('.')) {
|
61 |
+
setDisplay(display + '.');
|
62 |
+
}
|
63 |
+
};
|
64 |
+
|
65 |
+
const clear = () => {
|
66 |
+
setDisplay('0');
|
67 |
+
setFirstOperand(null);
|
68 |
+
setOperator(null);
|
69 |
+
setWaitingForSecondOperand(false);
|
70 |
+
};
|
71 |
+
|
72 |
+
const calculate = (firstOp, secondOp, op) => {
|
73 |
+
switch (op) {
|
74 |
+
case '+': return firstOp + secondOp;
|
75 |
+
case '-': return firstOp - secondOp;
|
76 |
+
case '*': return firstOp * secondOp;
|
77 |
+
case '/': return firstOp / secondOp;
|
78 |
+
default: return secondOp;
|
79 |
+
}
|
80 |
+
};
|
81 |
+
|
82 |
+
const performOperation = (nextOperator) => {
|
83 |
+
const inputValue = parseFloat(display);
|
84 |
+
|
85 |
+
if (firstOperand === null) {
|
86 |
+
setFirstOperand(inputValue);
|
87 |
+
} else if (operator) {
|
88 |
+
const result = calculate(firstOperand, inputValue, operator);
|
89 |
+
setDisplay(String(result));
|
90 |
+
setFirstOperand(result);
|
91 |
+
}
|
92 |
+
|
93 |
+
setWaitingForSecondOperand(true);
|
94 |
+
setOperator(nextOperator);
|
95 |
+
};
|
96 |
+
|
97 |
+
return (
|
98 |
+
<div className="bg-gray-100 p-4 rounded-lg shadow-lg w-full max-w-md">
|
99 |
+
<div className="flex justify-between items-center mb-4">
|
100 |
+
<h2 className="text-xl font-bold">Calculator</h2>
|
101 |
+
<button onClick={onClose} className="text-gray-600 hover:text-gray-800">
|
102 |
+
<X className="w-6 h-6" />
|
103 |
+
</button>
|
104 |
+
</div>
|
105 |
+
<div className="bg-gray-200 p-4 rounded mb-4 text-right text-2xl font-bold">
|
106 |
+
{display}
|
107 |
+
</div>
|
108 |
+
<div className="grid grid-cols-4 gap-2">
|
109 |
+
<button onClick={clear} className="p-3 bg-gray-300 hover:bg-gray-400 rounded">C</button>
|
110 |
+
<button onClick={() => performOperation('/')} className="p-3 bg-gray-300 hover:bg-gray-400 rounded">/</button>
|
111 |
+
<button onClick={() => performOperation('*')} className="p-3 bg-gray-300 hover:bg-gray-400 rounded">*</button>
|
112 |
+
<button onClick={() => performOperation('-')} className="p-3 bg-gray-300 hover:bg-gray-400 rounded">-</button>
|
113 |
+
|
114 |
+
<button onClick={() => inputDigit('7')} className="p-3 bg-white hover:bg-gray-100 rounded">7</button>
|
115 |
+
<button onClick={() => inputDigit('8')} className="p-3 bg-white hover:bg-gray-100 rounded">8</button>
|
116 |
+
<button onClick={() => inputDigit('9')} className="p-3 bg-white hover:bg-gray-100 rounded">9</button>
|
117 |
+
<button onClick={() => performOperation('+')} className="p-3 bg-gray-300 hover:bg-gray-400 rounded">+</button>
|
118 |
+
|
119 |
+
<button onClick={() => inputDigit('4')} className="p-3 bg-white hover:bg-gray-100 rounded">4</button>
|
120 |
+
<button onClick={() => inputDigit('5')} className="p-3 bg-white hover:bg-gray-100 rounded">5</button>
|
121 |
+
<button onClick={() => inputDigit('6')} className="p-3 bg-white hover:bg-gray-100 rounded">6</button>
|
122 |
+
<button onClick={() => performOperation('=')} className="p-3 bg-blue-500 hover:bg-blue-600 text-white rounded row-span-2">=</button>
|
123 |
+
|
124 |
+
<button onClick={() => inputDigit('1')} className="p-3 bg-white hover:bg-gray-100 rounded">1</button>
|
125 |
+
<button onClick={() => inputDigit('2')} className="p-3 bg-white hover:bg-gray-100 rounded">2</button>
|
126 |
+
<button onClick={() => inputDigit('3')} className="p-3 bg-white hover:bg-gray-100 rounded">3</button>
|
127 |
+
|
128 |
+
<button onClick={() => inputDigit('0')} className="p-3 bg-white hover:bg-gray-100 rounded col-span-2">0</button>
|
129 |
+
<button onClick={inputDecimal} className="p-3 bg-white hover:bg-gray-100 rounded">.</button>
|
130 |
+
</div>
|
131 |
+
</div>
|
132 |
+
);
|
133 |
+
};
|
134 |
+
|
135 |
const MathQuizApp = () => {
|
136 |
const [question, setQuestion] = useState(generateQuestion('easy'));
|
137 |
const [userAnswer, setUserAnswer] = useState('');
|
|
|
139 |
const [score, setScore] = useState(0);
|
140 |
const [wrongAnswers, setWrongAnswers] = useState(0);
|
141 |
const [difficulty, setDifficulty] = useState('easy');
|
142 |
+
const [showCalculator, setShowCalculator] = useState(false);
|
143 |
|
144 |
+
const handleSubmit = (e) => {
|
145 |
e.preventDefault();
|
146 |
if (Number(userAnswer) === question.answer) {
|
147 |
setResult('Correct!');
|
|
|
154 |
setQuestion(generateQuestion(difficulty));
|
155 |
};
|
156 |
|
157 |
+
const handleDifficultyChange = (e) => {
|
158 |
setDifficulty(e.target.value);
|
159 |
setQuestion(generateQuestion(e.target.value));
|
160 |
setScore(0);
|
|
|
162 |
};
|
163 |
|
164 |
return (
|
165 |
+
<div className="bg-gray-50 min-h-screen flex items-center justify-center p-4">
|
166 |
+
<div className="max-w-4xl w-full mx-auto bg-white rounded-lg shadow-md p-8">
|
167 |
+
<div className="flex justify-between items-center mb-6">
|
168 |
+
<h1 className="text-4xl font-bold">Math Quiz App</h1>
|
169 |
+
<button
|
170 |
+
onClick={() => setShowCalculator(!showCalculator)}
|
171 |
+
className="flex items-center gap-2 bg-blue-600 hover:bg-blue-700 text-white px-4 py-2 rounded-lg"
|
172 |
+
>
|
173 |
+
<Calculator className="w-5 h-5" />
|
174 |
+
{showCalculator ? 'Hide Calculator' : 'Show Calculator'}
|
175 |
+
</button>
|
176 |
+
</div>
|
177 |
+
|
178 |
+
<div className="grid grid-cols-1 md:grid-cols-2 gap-8">
|
179 |
+
<div className="space-y-6">
|
180 |
+
<form onSubmit={handleSubmit} className="space-y-6">
|
181 |
+
<div className="flex flex-col items-center gap-6">
|
182 |
+
<div className="flex gap-8 items-center justify-center">
|
183 |
+
<div className="text-xl font-bold">Correct: {score}</div>
|
184 |
+
<div className="text-xl font-bold text-red-500">Wrong: {wrongAnswers}</div>
|
185 |
+
</div>
|
186 |
+
<select
|
187 |
+
value={difficulty}
|
188 |
+
onChange={handleDifficultyChange}
|
189 |
+
className="bg-gray-50 border border-gray-300 text-gray-900 text-lg rounded-lg focus:ring-blue-500 focus:border-blue-500 p-2.5 w-48 text-center"
|
190 |
+
>
|
191 |
+
{difficulties.map((diff) => (
|
192 |
+
<option key={diff} value={diff}>
|
193 |
+
{diff.charAt(0).toUpperCase() + diff.slice(1)}
|
194 |
+
</option>
|
195 |
+
))}
|
196 |
+
</select>
|
197 |
+
</div>
|
198 |
+
<div className="text-center">
|
199 |
+
<p className="text-3xl font-bold mb-6">
|
200 |
+
What is {question.num1} {question.operation} {question.num2}?
|
201 |
+
</p>
|
202 |
+
<input
|
203 |
+
type="number"
|
204 |
+
value={userAnswer}
|
205 |
+
onChange={(e) => setUserAnswer(e.target.value)}
|
206 |
+
className="bg-gray-50 border border-gray-300 text-gray-900 text-xl rounded-lg focus:ring-blue-500 focus:border-blue-500 p-4 w-full text-center"
|
207 |
+
/>
|
208 |
+
</div>
|
209 |
+
<div className="text-center">
|
210 |
+
<button
|
211 |
+
type="submit"
|
212 |
+
className="text-white bg-blue-700 hover:bg-blue-800 focus:ring-4 focus:ring-blue-300 font-medium rounded-lg text-lg px-8 py-3 text-center"
|
213 |
+
>
|
214 |
+
Submit
|
215 |
+
</button>
|
216 |
+
</div>
|
217 |
+
{result && (
|
218 |
+
<p
|
219 |
+
className={`text-xl font-bold text-center ${
|
220 |
+
result === 'Correct!' ? 'text-green-500' : 'text-red-500'
|
221 |
+
}`}
|
222 |
+
>
|
223 |
+
{result}{' '}
|
224 |
+
{result === 'Correct!' ? (
|
225 |
+
<Check className="inline-block w-6 h-6" />
|
226 |
+
) : (
|
227 |
+
<X className="inline-block w-6 h-6" />
|
228 |
+
)}
|
229 |
+
</p>
|
230 |
)}
|
231 |
+
</form>
|
232 |
+
</div>
|
233 |
+
|
234 |
+
{showCalculator && (
|
235 |
+
<div className="flex justify-center">
|
236 |
+
<CalculatorComponent onClose={() => setShowCalculator(false)} />
|
237 |
+
</div>
|
238 |
)}
|
239 |
+
</div>
|
240 |
</div>
|
241 |
</div>
|
242 |
);
|
243 |
};
|
244 |
|
245 |
+
export default MathQuizApp;
|