From 21414b49c86ca8ba6bbc36d09dcdc282cc1ae943 Mon Sep 17 00:00:00 2001 From: Ibrahim Mkusa Date: Wed, 26 Apr 2023 22:53:54 -0400 Subject: ex 1.14 displays anecdote with most votes --- part1/anecdotes/src/App.js | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/part1/anecdotes/src/App.js b/part1/anecdotes/src/App.js index 8bc3b2b..3513cce 100644 --- a/part1/anecdotes/src/App.js +++ b/part1/anecdotes/src/App.js @@ -23,6 +23,7 @@ const App = () => { const [selected, setSelected] = useState(0) const [votes, setVotes] = useState(Array(anecdotes.length).fill(0)) + const [most, setMost] = useState(0) const getRandomInt = (min, max) => { @@ -37,12 +38,34 @@ const App = () => { return copy } + const findIndexOfMax = () => { + let indexMax = 0 + let maxValue = 0 + for (let i = 0, len = votes.length; i < len; i++) { + if (votes[i] > maxValue) { + indexMax = i + maxValue = votes[i] + } + + } + + return indexMax + } + return (
+

Anecdote of the day

{anecdotes[selected]}

has {votes[selected]} votes

-
) } -- cgit v1.2.3