scrimba
Speed Typing Game Part 5
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 5
AboutCommentsNotes
Speed Typing Game Part 5
Expand for more info
App.js
run
preview
console
import React, {useState, useEffect} from "react"

/**
* 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
Console
/index.html
-6:49