scrimba
React Bootcamp Course
Speed Typing Game Part 6
Go Pro!Bootcamp

Bootcamp

Study group

Collaborate with peers in your dedicated #study-group channel.

Code reviews

Submit projects for review using the /review command in your #code-reviews channel

Speed Typing Game Part 6
AboutCommentsNotes
Speed Typing Game Part 6
Expand for more info
App.js
run
preview
console
import React, {useState, useEffect} from "react"

/**
* Challenge:
*
* When the timer reaches 0, count the number of words the user typed in
* and display it in the "Word count" section
*/

function App() {
const [text, setText] = useState("")
const [timeRemaining, setTimeRemaining] = useState(5)
const [isTimeRunning, setIsTimeRunning] = useState(false)

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(isTimeRunning && timeRemaining > 0) {
setTimeout(() => {
setTimeRemaining(time => time - 1)
}, 1000)
} else if(timeRemaining === 0) {
setIsTimeRunning(false)
}
}, [timeRemaining, isTimeRunning])

return (
<div>
<h1>How fast do you type?</h1>
<textarea
onChange={handleChange}
value={text}
/>
<h4>Time remaining: {timeRemaining}</h4>
<button onClick={() => setIsTimeRunning(true)}>Start</button>
<h1>Word count: ???</h1>
</div>
)
}

export default App
Console
/index.html
-12:57