Creame un componente funcional de ReactJs de un juego que cuenta las palabras por minuto…
- User
Creame un componente funcional de ReactJs de un juego que cuenta las palabras por minuto que tipeo. El usuario al clickear en play, ve una palabra en pantalla y en el input tiene que introducirla mientras el tiempo corre. En el submit, se suma la longitud de palabras al puntaje inicial (0) y al terminar el tiempo se debe mostrar cuantos puntos obtuvo.
- Assistant
Aquí te dejo un ejemplo de cómo podría ser un componente funcional de ReactJs para el juego que describes:
import React, { useState, useRef } from 'react';
const WordGame = () => { const [score, setScore] = useState(0); const [word, setWord] = useState(''); const [time, setTime] = useState(60); const inputRef = useRef(null);
const wordsList = ['hola', 'mundo', 'react', 'javascript', 'programacion', 'desarrollo'];
const startGame = () => { setScore(0); setTime(60); setWord(wordsList[Math.floor(Math.random() * wordsList.length)]); inputRef.current.value = ''; inputRef.current.focus(); };
const handleSubmit = (e) => { e.preventDefault(); if (word === inputRef.current.value.trim()) { setScore(score + word.length); } setWord(wordsList[Math.floor(Math.random() * wordsList.length)]); inputRef.current.value = ''; inputRef.current.focus(); };
useEffect(() => { if (time === 0) { alert(); startGame(); } else { setTimeout(() => setTime(time - 1), 1000); } }, [time]);
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.