import React, { useState, useEffect, useRef } from 'react' import { motion, AnimatePresence } from 'framer-motion' export default function FinalJeopardy({ finalData, score, setScore, onComplete }) { const [showWager, setShowWager] = useState(true) const [showQuestion, setShowQuestion] = useState(false) const [showResult, setShowResult] = useState(false) const [wager, setWager] = useState('') const [userAnswer, setUserAnswer] = useState('') const [isCorrect, setIsCorrect] = useState(false) const [finalScore, setFinalScore] = useState(score) const correctAudioRef = useRef(null) const incorrectAudioRef = useRef(null) useEffect(() => { correctAudioRef.current = new Audio('/correct.mp3') incorrectAudioRef.current = new Audio('/incorrect.mp3') return () => { correctAudioRef.current?.pause() incorrectAudioRef.current?.pause() } }, []) useEffect(() => { if (showResult) { if (isCorrect) { correctAudioRef.current?.play().catch(() => {}) } else if (userAnswer) { incorrectAudioRef.current?.play().catch(() => {}) } } }, [showResult, isCorrect, userAnswer]) // Helper function to normalize answers for comparison const normalizeAnswer = (answer) => { if (!answer) return '' let normalized = answer.trim().toLowerCase().replace(/[^\w\s]/g, '') const questionPrefixes = [ /^what\s+is\s+/i, /^what\s+are\s+/i, /^who\s+is\s+/i, /^who\s+are\s+/i, /^where\s+is\s+/i, /^where\s+are\s+/i, /^when\s+is\s+/i, /^when\s+are\s+/i, /^how\s+is\s+/i, /^how\s+are\s+/i, /^which\s+is\s+/i, /^which\s+are\s+/i ] for (const prefix of questionPrefixes) { normalized = normalized.replace(prefix, '').trim() } // Remove leading articles (a, an, the) normalized = normalized.replace(/^(the|a|an)\s+/i, '').trim() return normalized.trim() } const compareAnswers = (userAns, correctAns) => { // Empty answers are always incorrect if (!userAns || !userAns.trim()) return false const userNorm = normalizeAnswer(userAns) const correctNorm = normalizeAnswer(correctAns) // If normalized user answer is empty, it's incorrect if (!userNorm || userNorm.length === 0) return false // Exact match if (userNorm === correctNorm) return true // Handle pluralization - remove trailing 's' and compare const userSingular = userNorm.replace(/s$/, '') const correctSingular = correctNorm.replace(/s$/, '') if (userSingular === correctNorm || userNorm === correctSingular) return true if (userSingular === correctSingular && userSingular.length > 0) return true // Check if singular forms match when one has 's' and other doesn't if (userNorm.endsWith('s') && userNorm.slice(0, -1) === correctNorm) return true if (correctNorm.endsWith('s') && correctNorm.slice(0, -1) === userNorm) return true // For multi-word answers, check if they match word-by-word (handles word order differences) const userWords = userNorm.split(/\s+/).sort().join(' ') const correctWords = correctNorm.split(/\s+/).sort().join(' ') if (userWords === correctWords && userWords.length > 0) return true return false } const handleWagerSubmit = () => { const wagerAmount = parseInt(wager) || 0 const maxWager = Math.max(0, score) // Can't wager negative const finalWager = Math.max(0, Math.min(wagerAmount, maxWager)) setWager(finalWager.toString()) setShowWager(false) setShowQuestion(true) } const handleSubmit = () => { const wagerAmount = parseInt(wager) || 0 const correct = compareAnswers(userAnswer, finalData.answer) setIsCorrect(correct) setShowResult(true) setScore(prevScore => { const newScore = correct ? prevScore + wagerAmount : prevScore - wagerAmount setFinalScore(newScore) return newScore }) } const handleContinue = () => { onComplete() } if (showWager) { return (