Update components/MathQuizApp.tsx
Browse files- components/MathQuizApp.tsx +32 -17
components/MathQuizApp.tsx
CHANGED
@@ -3,10 +3,20 @@
|
|
3 |
import { useState } from 'react';
|
4 |
import { Check, X, Calculator } from 'lucide-react';
|
5 |
|
6 |
-
const operations = ['+', '-', '*', '/'];
|
7 |
-
|
8 |
|
9 |
-
const
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
const operation = operations[Math.floor(Math.random() * operations.length)];
|
11 |
let num1 = 0;
|
12 |
let num2 = 0;
|
@@ -48,13 +58,17 @@ const generateQuestion = (difficulty) => {
|
|
48 |
};
|
49 |
};
|
50 |
|
51 |
-
|
|
|
|
|
|
|
|
|
52 |
const [display, setDisplay] = useState('0');
|
53 |
-
const [firstOperand, setFirstOperand] = useState(null);
|
54 |
-
const [operator, setOperator] = useState(null);
|
55 |
const [waitingForSecondOperand, setWaitingForSecondOperand] = useState(false);
|
56 |
|
57 |
-
const inputDigit = (digit) => {
|
58 |
if (waitingForSecondOperand) {
|
59 |
setDisplay(digit);
|
60 |
setWaitingForSecondOperand(false);
|
@@ -81,7 +95,7 @@ const CalculatorComponent = ({ onClose }) => {
|
|
81 |
setWaitingForSecondOperand(false);
|
82 |
};
|
83 |
|
84 |
-
const calculate = (firstOp, secondOp, op) => {
|
85 |
switch (op) {
|
86 |
case '+': return firstOp + secondOp;
|
87 |
case '-': return firstOp - secondOp;
|
@@ -91,7 +105,7 @@ const CalculatorComponent = ({ onClose }) => {
|
|
91 |
}
|
92 |
};
|
93 |
|
94 |
-
const performOperation = (nextOperator) => {
|
95 |
const inputValue = parseFloat(display);
|
96 |
|
97 |
if (nextOperator === '=') {
|
@@ -108,7 +122,7 @@ const CalculatorComponent = ({ onClose }) => {
|
|
108 |
} else if (operator) {
|
109 |
const result = calculate(firstOperand, inputValue, operator);
|
110 |
setDisplay(String(result));
|
111 |
-
setFirstOperand(result);
|
112 |
}
|
113 |
|
114 |
setWaitingForSecondOperand(true);
|
@@ -154,16 +168,16 @@ const CalculatorComponent = ({ onClose }) => {
|
|
154 |
);
|
155 |
};
|
156 |
|
157 |
-
const MathQuizApp = () => {
|
158 |
-
const [question, setQuestion] = useState(generateQuestion('easy'));
|
159 |
const [userAnswer, setUserAnswer] = useState('');
|
160 |
const [result, setResult] = useState('');
|
161 |
const [score, setScore] = useState(0);
|
162 |
const [wrongAnswers, setWrongAnswers] = useState(0);
|
163 |
-
const [difficulty, setDifficulty] = useState('easy');
|
164 |
const [showCalculator, setShowCalculator] = useState(false);
|
165 |
|
166 |
-
const handleSubmit = (e) => {
|
167 |
e.preventDefault();
|
168 |
|
169 |
// Parse the user answer as a float and check against the question answer
|
@@ -184,9 +198,10 @@ const MathQuizApp = () => {
|
|
184 |
setQuestion(generateQuestion(difficulty));
|
185 |
};
|
186 |
|
187 |
-
const handleDifficultyChange = (e) => {
|
188 |
-
|
189 |
-
|
|
|
190 |
setScore(0);
|
191 |
setWrongAnswers(0);
|
192 |
setResult('');
|
|
|
3 |
import { useState } from 'react';
|
4 |
import { Check, X, Calculator } from 'lucide-react';
|
5 |
|
6 |
+
const operations = ['+', '-', '*', '/'] as const;
|
7 |
+
type Operation = typeof operations[number];
|
8 |
|
9 |
+
const difficulties = ['easy', 'medium', 'hard'] as const;
|
10 |
+
type Difficulty = typeof difficulties[number];
|
11 |
+
|
12 |
+
interface Question {
|
13 |
+
num1: number;
|
14 |
+
num2: number;
|
15 |
+
operation: Operation;
|
16 |
+
answer: number;
|
17 |
+
}
|
18 |
+
|
19 |
+
const generateQuestion = (difficulty: Difficulty): Question => {
|
20 |
const operation = operations[Math.floor(Math.random() * operations.length)];
|
21 |
let num1 = 0;
|
22 |
let num2 = 0;
|
|
|
58 |
};
|
59 |
};
|
60 |
|
61 |
+
interface CalculatorComponentProps {
|
62 |
+
onClose: () => void;
|
63 |
+
}
|
64 |
+
|
65 |
+
const CalculatorComponent: React.FC<CalculatorComponentProps> = ({ onClose }) => {
|
66 |
const [display, setDisplay] = useState('0');
|
67 |
+
const [firstOperand, setFirstOperand] = useState<number | null>(null);
|
68 |
+
const [operator, setOperator] = useState<string | null>(null);
|
69 |
const [waitingForSecondOperand, setWaitingForSecondOperand] = useState(false);
|
70 |
|
71 |
+
const inputDigit = (digit: string) => {
|
72 |
if (waitingForSecondOperand) {
|
73 |
setDisplay(digit);
|
74 |
setWaitingForSecondOperand(false);
|
|
|
95 |
setWaitingForSecondOperand(false);
|
96 |
};
|
97 |
|
98 |
+
const calculate = (firstOp: number, secondOp: number, op: string): number | string => {
|
99 |
switch (op) {
|
100 |
case '+': return firstOp + secondOp;
|
101 |
case '-': return firstOp - secondOp;
|
|
|
105 |
}
|
106 |
};
|
107 |
|
108 |
+
const performOperation = (nextOperator: string) => {
|
109 |
const inputValue = parseFloat(display);
|
110 |
|
111 |
if (nextOperator === '=') {
|
|
|
122 |
} else if (operator) {
|
123 |
const result = calculate(firstOperand, inputValue, operator);
|
124 |
setDisplay(String(result));
|
125 |
+
setFirstOperand(typeof result === 'number' ? result : null);
|
126 |
}
|
127 |
|
128 |
setWaitingForSecondOperand(true);
|
|
|
168 |
);
|
169 |
};
|
170 |
|
171 |
+
const MathQuizApp: React.FC = () => {
|
172 |
+
const [question, setQuestion] = useState<Question>(generateQuestion('easy'));
|
173 |
const [userAnswer, setUserAnswer] = useState('');
|
174 |
const [result, setResult] = useState('');
|
175 |
const [score, setScore] = useState(0);
|
176 |
const [wrongAnswers, setWrongAnswers] = useState(0);
|
177 |
+
const [difficulty, setDifficulty] = useState<Difficulty>('easy');
|
178 |
const [showCalculator, setShowCalculator] = useState(false);
|
179 |
|
180 |
+
const handleSubmit = (e: React.FormEvent) => {
|
181 |
e.preventDefault();
|
182 |
|
183 |
// Parse the user answer as a float and check against the question answer
|
|
|
198 |
setQuestion(generateQuestion(difficulty));
|
199 |
};
|
200 |
|
201 |
+
const handleDifficultyChange = (e: React.ChangeEvent<HTMLSelectElement>) => {
|
202 |
+
const newDifficulty = e.target.value as Difficulty;
|
203 |
+
setDifficulty(newDifficulty);
|
204 |
+
setQuestion(generateQuestion(newDifficulty));
|
205 |
setScore(0);
|
206 |
setWrongAnswers(0);
|
207 |
setResult('');
|