'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 = ({ onClose }) => { const [display, setDisplay] = useState('0'); const [firstOperand, setFirstOperand] = useState(null); const [operator, setOperator] = useState(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 (

Calculator

{display}
); }; const MathQuizApp: React.FC = () => { const [question, setQuestion] = useState(generateQuestion('easy')); const [userAnswer, setUserAnswer] = useState(''); const [result, setResult] = useState(''); const [score, setScore] = useState(0); const [wrongAnswers, setWrongAnswers] = useState(0); const [difficulty, setDifficulty] = useState('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) => { const newDifficulty = e.target.value as Difficulty; setDifficulty(newDifficulty); setQuestion(generateQuestion(newDifficulty)); setScore(0); setWrongAnswers(0); setResult(''); }; return (

Math Quiz App

Correct: {score}
Wrong: {wrongAnswers}

What is {question.num1} {question.operation} {question.num2}?

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" />
{result && (

{result}{' '} {result === 'Correct!' ? ( ) : ( )}

)}
{showCalculator && (
setShowCalculator(false)} />
)}
); }; export default MathQuizApp;