exercise 1.13

This commit is contained in:
Anish Lakhwara 2023-05-15 22:12:23 +10:00
parent 48736417d6
commit 030b96edad

View file

@ -21,16 +21,25 @@ const App = () => {
]
const [selected, setSelected] = useState(0)
const [scores, setScores] = useState(Array(anecdotes.length).fill(0))
const randomText = () => {
const value = Math.floor(Math.random() * anecdotes.length)
setSelected(value)
}
const VoteUp = () => {
const copy = [...scores]
copy[selected] += 1
setScores(copy)
}
return (
<div>
<Button handler={randomText} text="Randomize" />
{anecdotes[selected]}
<Button handler={VoteUp} text="Vote" />
<p>{anecdotes[selected]}</p>
<p>Votes: {scores[selected]}</p>
</div>
)
}