compelted 1.14

This commit is contained in:
Pablo Martin 2025-05-20 15:13:36 +02:00
parent 53abf66608
commit f741853323
2 changed files with 17 additions and 1 deletions

View file

@ -30,11 +30,27 @@ const App = (props) => {
setSelected(Math.floor(Math.random() * props.anecdotes.length))
}
const getMostVotedIndex = (votesArray) => {
let highestVoteCount = 0;
let highestVoteIndex;
for (let i = 0; i < votesArray.length; i++) {
if (votesArray[i] >= highestVoteCount) {
highestVoteIndex = i
highestVoteCount = votesArray[i]
}
}
return highestVoteIndex;
}
return (
<>
<AnecdoteDisplay anecdotes={props.anecdotes} anecdoteIndex={selected} anecdoteVotes={votes[selected]}/>
<Button buttonText={'Vote this'} onClickCallback={voteQuote}/>
<Button buttonText={'Next'} onClickCallback={selectRandomAnecdote}/>
<h1>The most popular anecdote</h1>
<AnecdoteDisplay anecdotes={props.anecdotes} anecdoteIndex={getMostVotedIndex(votes)} anecdoteVotes={votes[getMostVotedIndex(votes)]}/>
</>
)
}