scrimba
Frontend Career Path
React basics
Notes & Tenzies
Tenzies: End game part 2
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

AboutCommentsNotes
Tenzies: End game part 2
Expand for more info
App.js
run
preview
console
import React from "react"
import Die from "./Die"
import {nanoid} from "nanoid"

export default function App() {

const [dice, setDice] = React.useState(allNewDice())
const [tenzies, setTenzies] = React.useState(false)

React.useEffect(() => {
console.log("Dice state changed")
}, [dice])
/**
* Challenge: Check the dice array for these winning conditions:
* 1. All dice are held, and
* 2. all dice have the same value
*
* If both conditions are true, set `tenzies` to true and log
* "You won!" to the console
*/

function generateNewDie() {
return {
value: Math.ceil(Math.random() * 6),
isHeld: false,
id: nanoid()
}
}

function allNewDice() {
const newDice = []
for (let i = 0; i < 10; i++) {
newDice.push(generateNewDie())
}
return newDice
}


function rollDice() {
setDice(oldDice => oldDice.map(die => {
return die.isHeld ?
die :
generateNewDie()
}))
}

function holdDice(id) {
setDice(oldDice => oldDice.map(die => {
return die.id === id ?
{...die, isHeld: !die.isHeld} :
die
}))
}

const diceElements = dice.map(die => (
<Die
key={die.id}
value={die.value}
isHeld={die.isHeld}
holdDice={() => holdDice(die.id)}
/>
))

return (
<main>
<h1 className="title">Tenzies</h1>
<p className="instructions">Roll until all dice are the same. Click each die to freeze it at its current value between rolls.</p>
<div className="dice-container">
{diceElements}
</div>
<button className="roll-dice" onClick={rollDice}>Roll</button>
</main>
)
}
Console
"Dice state changed"
,
/index.html
-4:55