1

How can I get the highest number of votes in the following arrays ? I tried to have a look on the net including stackoverflow about how to find the highest value in an array and I tried applying it like this,

const sortHighestVotes = Math.max.apply(Math, votes.map(function(o) { return o.vote ; }))

I got no errors but the value displayed was NaN. I think that this is because of the attribute which is introduced in the handleVotes component. This is different from most of references that I referred to where attribute is already defined in a "key: value" pair in their array/object. A little bit different from my situation. The following are my code. How can I fix this ?

const App = () => {
  const handleClick2 = () => {
    setSelected(Math.floor(Math.random()*anecdotes.length))
  }

  const handleVotes = () => { 
    setVotes((votes) => 
      votes.map((vote, index) => index === selected ? vote + 1 : vote)
    )
  }

  const anecdotes = [
    'If it hurts, do it more often.',
    'Adding manpower to a late software project makes it later!',
    'The first 90 percent of the code accounts for the first 10 percent of the development time...The remaining 10 percent of the code accounts for the other 90 percent of the development time.',
  ]
   
  const [selected, setSelected] = useState(0)
  const [votes, setVotes] = useState(() => Array(anecdotes.length).fill(0))

  const sortHighestVotes = Math.max.apply(Math, votes.map(function(o) { return o.vote ; }))

  return (

    <div style={inlineParagraph}>
      <h1>Anecdote of the day</h1>

      { anecdotes[selected] }

      <div style={inlineButton} >
        <Button handleClick={handleClick2} text="next anecdotes" setSelected={setSelected} />
        <Button handleClick={handleVotes} text="vote" setSelected={setSelected}/> : {votes[selected]}
      </div>

      <h1>Anecdote with the most votes</h1>
      <p> { sortHighestVotes }  </p> 
      
    </div>
  )
}

export default App
7
  • do you have always numbers in your vote property? Commented Feb 11, 2023 at 8:42
  • @NinaScholz yes, i can increase the votes for different anecdotes and it is working well but i want to be able to extract the anecdote that has the highest number of votes and display it using {sortHighestVotes} Commented Feb 11, 2023 at 8:45
  • could yxou have empty arrays? Commented Feb 11, 2023 at 8:46
  • @NinaScholz if i console.log(votes), i'd have arrays with (3) [0, 0, 0] which will change as i increase the votes. so it could become (3) [1, 2 , 5] or any number as I change the votes Commented Feb 11, 2023 at 8:49
  • @NinaScholz I tried Math.max(...votes) and now it is showing the highest number of votes. but how can i include i display the anecdote that holds the highest votes as well ? Commented Feb 11, 2023 at 9:07

1 Answer 1

1

Please try Math.max(...votes);. You need to spread the array to get each value, then Math.max will return you the highest value. Just be careful to check if the array exists and has numbers inside.

EDIT: If you want to find the index of the highest vote anectote. You can do:

 const maxVoteIndex = votes.indexOf(Math.max(...votes));
 const maxVoteAnecdote = anecdotes[maxVoteIndex];
Sign up to request clarification or add additional context in comments.

3 Comments

this works ! now it is showing the highest number of votes. but how can i display the anecdote that comes with it ?
you can try with indexOf to get the index of the max vote. Then, use that index to get the anecdote.
works like a charm ! I defined 2 variables, const sortHighestVotes = Math.max(...votes) and const anecdoteWithHighestVotes = votes.indexOf(sortHighestVotes). after that, i displayed it . thanks for pointing me in the right direction

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.