File size: 11,217 Bytes
a4ec883
 
 
bb4cc54
a4ec883
355411b
 
a4ec883
355411b
 
 
 
 
 
 
 
 
 
 
a4ec883
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4fb37df
 
 
 
 
 
 
 
 
 
 
 
 
 
a4ec883
 
 
 
4fb37df
a4ec883
 
 
355411b
 
 
 
 
bb4cc54
355411b
 
bb4cc54
 
355411b
bb4cc54
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
355411b
bb4cc54
 
 
 
4fb37df
bb4cc54
 
 
 
355411b
bb4cc54
 
4fb37df
 
 
 
 
 
 
 
 
 
 
 
 
 
355411b
4fb37df
 
 
 
bb4cc54
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
355411b
 
a4ec883
 
 
74378ac
355411b
bb4cc54
a4ec883
355411b
a4ec883
4fb37df
 
 
 
 
 
 
 
a4ec883
 
 
 
74378ac
a4ec883
4fb37df
a4ec883
 
 
 
355411b
 
 
 
a4ec883
74378ac
4fb37df
a4ec883
 
 
bb4cc54
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4fb37df
bb4cc54
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a49e052
bb4cc54
 
 
 
 
 
 
a49e052
bb4cc54
a49e052
a4ec883
 
 
 
bb4cc54
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
'use client';

import { useState } from 'react';
import { Check, X, Calculator } from 'lucide-react';

const operations = ['+', '-', '*', '/'] as const;
type Operation = typeof operations[number];

const difficulties = ['easy', 'medium', 'hard'] as const;
type Difficulty = typeof difficulties[number];

interface Question {
  num1: number;
  num2: number;
  operation: Operation;
  answer: number;
}

const generateQuestion = (difficulty: Difficulty): Question => {
  const operation = operations[Math.floor(Math.random() * operations.length)];
  let num1 = 0;
  let num2 = 0;

  switch (difficulty) {
    case 'easy':
      num1 = Math.floor(Math.random() * 10) + 1;
      num2 = Math.floor(Math.random() * 10) + 1;
      break;
    case 'medium':
      num1 = Math.floor(Math.random() * 50) + 1;
      num2 = Math.floor(Math.random() * 50) + 1;
      break;
    case 'hard':
      num1 = Math.floor(Math.random() * 100) + 1;
      num2 = Math.floor(Math.random() * 100) + 1;
      break;
  }

  // For division, ensure it results in a whole number or simpler decimal
  if (operation === '/') {
    // Make sure num2 is not zero to avoid division by zero
    num2 = num2 === 0 ? 1 : num2;
    // For easier difficulty levels, try to make divisions that result in whole numbers
    if (difficulty === 'easy' || difficulty === 'medium') {
      num1 = num2 * Math.floor(Math.random() * 10) + 1;
    }
  }

  const answer = eval(`${num1} ${operation} ${num2}`);
  // Round the answer to 2 decimal places if it's a float
  const roundedAnswer = Number.isInteger(answer) ? answer : parseFloat(answer.toFixed(2));

  return {
    num1,
    num2,
    operation,
    answer: roundedAnswer,
  };
};

interface CalculatorComponentProps {
  onClose: () => void;
}

const CalculatorComponent: React.FC<CalculatorComponentProps> = ({ onClose }) => {
  const [display, setDisplay] = useState('0');
  const [firstOperand, setFirstOperand] = useState<number | null>(null);
  const [operator, setOperator] = useState<string | null>(null);
  const [waitingForSecondOperand, setWaitingForSecondOperand] = useState(false);

  const inputDigit = (digit: string) => {
    if (waitingForSecondOperand) {
      setDisplay(digit);
      setWaitingForSecondOperand(false);
    } else {
      setDisplay(display === '0' ? digit : display + digit);
    }
  };

  const inputDecimal = () => {
    if (waitingForSecondOperand) {
      setDisplay('0.');
      setWaitingForSecondOperand(false);
      return;
    }
    if (!display.includes('.')) {
      setDisplay(display + '.');
    }
  };

  const clear = () => {
    setDisplay('0');
    setFirstOperand(null);
    setOperator(null);
    setWaitingForSecondOperand(false);
  };

  const calculate = (firstOp: number, secondOp: number, op: string): number | string => {
    switch (op) {
      case '+': return firstOp + secondOp;
      case '-': return firstOp - secondOp;
      case '*': return firstOp * secondOp;
      case '/': return secondOp === 0 ? 'Error' : firstOp / secondOp;
      default: return secondOp;
    }
  };

  const performOperation = (nextOperator: string) => {
    const inputValue = parseFloat(display);
    
    if (nextOperator === '=') {
      if (operator && firstOperand !== null) {
        const result = calculate(firstOperand, inputValue, operator);
        setDisplay(String(result));
        setFirstOperand(null);
        setOperator(null);
        setWaitingForSecondOperand(false);
      }
    } else {
      if (firstOperand === null) {
        setFirstOperand(inputValue);
      } else if (operator) {
        const result = calculate(firstOperand, inputValue, operator);
        setDisplay(String(result));
        setFirstOperand(typeof result === 'number' ? result : null);
      }
      
      setWaitingForSecondOperand(true);
      setOperator(nextOperator);
    }
  };

  return (
    <div className="bg-gray-100 p-4 rounded-lg shadow-lg w-full max-w-md">
      <div className="flex justify-between items-center mb-4">
        <h2 className="text-xl font-bold">Calculator</h2>
        <button onClick={onClose} className="text-gray-600 hover:text-gray-800">
          <X className="w-6 h-6" />
        </button>
      </div>
      <div className="bg-gray-200 p-4 rounded mb-4 text-right text-2xl font-bold">
        {display}
      </div>
      <div className="grid grid-cols-4 gap-2">
        <button onClick={clear} className="p-3 bg-gray-300 hover:bg-gray-400 rounded">C</button>
        <button onClick={() => performOperation('/')} className="p-3 bg-gray-300 hover:bg-gray-400 rounded">/</button>
        <button onClick={() => performOperation('*')} className="p-3 bg-gray-300 hover:bg-gray-400 rounded">*</button>
        <button onClick={() => performOperation('-')} className="p-3 bg-gray-300 hover:bg-gray-400 rounded">-</button>
        
        <button onClick={() => inputDigit('7')} className="p-3 bg-white hover:bg-gray-100 rounded">7</button>
        <button onClick={() => inputDigit('8')} className="p-3 bg-white hover:bg-gray-100 rounded">8</button>
        <button onClick={() => inputDigit('9')} className="p-3 bg-white hover:bg-gray-100 rounded">9</button>
        <button onClick={() => performOperation('+')} className="p-3 bg-gray-300 hover:bg-gray-400 rounded">+</button>
        
        <button onClick={() => inputDigit('4')} className="p-3 bg-white hover:bg-gray-100 rounded">4</button>
        <button onClick={() => inputDigit('5')} className="p-3 bg-white hover:bg-gray-100 rounded">5</button>
        <button onClick={() => inputDigit('6')} className="p-3 bg-white hover:bg-gray-100 rounded">6</button>
        <button onClick={() => performOperation('=')} className="p-3 bg-blue-500 hover:bg-blue-600 text-white rounded row-span-2">=</button>
        
        <button onClick={() => inputDigit('1')} className="p-3 bg-white hover:bg-gray-100 rounded">1</button>
        <button onClick={() => inputDigit('2')} className="p-3 bg-white hover:bg-gray-100 rounded">2</button>
        <button onClick={() => inputDigit('3')} className="p-3 bg-white hover:bg-gray-100 rounded">3</button>
        
        <button onClick={() => inputDigit('0')} className="p-3 bg-white hover:bg-gray-100 rounded col-span-2">0</button>
        <button onClick={inputDecimal} className="p-3 bg-white hover:bg-gray-100 rounded">.</button>
      </div>
    </div>
  );
};

const MathQuizApp: React.FC = () => {
  const [question, setQuestion] = useState<Question>(generateQuestion('easy'));
  const [userAnswer, setUserAnswer] = useState('');
  const [result, setResult] = useState('');
  const [score, setScore] = useState(0);
  const [wrongAnswers, setWrongAnswers] = useState(0);
  const [difficulty, setDifficulty] = useState<Difficulty>('easy');
  const [showCalculator, setShowCalculator] = useState(false);

  const handleSubmit = (e: React.FormEvent) => {
    e.preventDefault();
    
    // Parse the user answer as a float and check against the question answer
    const parsedAnswer = parseFloat(userAnswer);
    
    // Check if the answer matches (with some tolerance for floating point)
    const isCorrect = Math.abs(parsedAnswer - question.answer) < 0.001;
    
    if (isCorrect) {
      setResult('Correct!');
      setScore(score + 1);
    } else {
      setResult(`Incorrect. The correct answer is ${question.answer}.`);
      setWrongAnswers(wrongAnswers + 1);
    }
    
    setUserAnswer('');
    setQuestion(generateQuestion(difficulty));
  };

  const handleDifficultyChange = (e: React.ChangeEvent<HTMLSelectElement>) => {
    const newDifficulty = e.target.value as Difficulty;
    setDifficulty(newDifficulty);
    setQuestion(generateQuestion(newDifficulty));
    setScore(0);
    setWrongAnswers(0);
    setResult('');
  };

  return (
    <div className="bg-gray-50 min-h-screen flex items-center justify-center p-4">
      <div className="max-w-4xl w-full mx-auto bg-white rounded-lg shadow-md p-8">
        <div className="flex justify-between items-center mb-6">
          <h1 className="text-4xl font-bold">Math Quiz App</h1>
          <button
            onClick={() => setShowCalculator(!showCalculator)}
            className="flex items-center gap-2 bg-blue-600 hover:bg-blue-700 text-white px-4 py-2 rounded-lg"
          >
            <Calculator className="w-5 h-5" />
            {showCalculator ? 'Hide Calculator' : 'Show Calculator'}
          </button>
        </div>
        <div className="grid grid-cols-1 md:grid-cols-2 gap-8">
          <div className="space-y-6">
            <form onSubmit={handleSubmit} className="space-y-6">
              <div className="flex flex-col items-center gap-6">
                <div className="flex gap-8 items-center justify-center">
                  <div className="text-xl font-bold">Correct: {score}</div>
                  <div className="text-xl font-bold text-red-500">Wrong: {wrongAnswers}</div>
                </div>
                <select
                  value={difficulty}
                  onChange={handleDifficultyChange}
                  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"
                >
                  {difficulties.map((diff) => (
                    <option key={diff} value={diff}>
                      {diff.charAt(0).toUpperCase() + diff.slice(1)}
                    </option>
                  ))}
                </select>
              </div>
              <div className="text-center">
                <p className="text-3xl font-bold mb-6">
                  What is {question.num1} {question.operation} {question.num2}?
                </p>
                <input
                  type="number"
                  value={userAnswer}
                  onChange={(e) => setUserAnswer(e.target.value)}
                  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"
                  step="any"
                />
              </div>
              <div className="text-center">
                <button
                  type="submit"
                  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"
                >
                  Submit
                </button>
              </div>
              {result && (
                <p
                  className={`text-xl font-bold text-center ${
                    result === 'Correct!' ? 'text-green-500' : 'text-red-500'
                  }`}
                >
                  {result}{' '}
                  {result === 'Correct!' ? (
                    <Check className="inline-block w-6 h-6" />
                  ) : (
                    <X className="inline-block w-6 h-6" />
                  )}
                </p>
              )}
            </form>
          </div>
          
          {showCalculator && (
            <div className="flex justify-center">
              <CalculatorComponent onClose={() => setShowCalculator(false)} />
            </div>
          )}
        </div>
      </div>
    </div>
  );
};

export default MathQuizApp;