/**
* Challenge:
*
* Make it so clicking the Start button starts the timer instead of it starting on refresh
* (Hint: use a new state variable to indicate if the game should be running or not)
*/
function App() {
const [text, setText] = useState("")
const [timeRemaining, setTimeRemaining] = useState(5)
function handleChange(e) {
const {value} = e.target
setText(value)
}
function calculateWordCount(text) {
const wordsArr = text.trim().split(" ")
return wordsArr.filter(word => word !== "").length
}
useEffect(() => {
if(timeRemaining > 0) {
setTimeout(() => {
setTimeRemaining(time => time - 1)
}, 1000)
}
}, [timeRemaining])
return (
<div>
<h1>How fast do you type?</h1>
<textarea
onChange={handleChange}
value={text}
/>
<h4>Time remaining: {timeRemaining}</h4>
<button onClick={() => console.log(calculateWordCount(text))}>Start</button>
<h1>Word count: ???</h1>
</div>
)
}
export default App
import React, {useState, useEffect} from "react"