1

I'm trying to write a vote counting function, I have an array of objects and each object has a property called currentVote which has a userId value. I want to find out which userID has the most votes. Using lodash this is what I got so far:

function countVotes(players) {
  return new Promise(function(resolve, reject) {
    //votes should be empty, filled it with some sample values
    let votes = ['312139659792875521','360445341989863434', '312139659792875521','360445341989863435','1','9999999999999999999999'];
    for (let i = 0, j = players.length; i < j; i++) {
      votes.push(players[i].currentVote);
    }
    let tally = _.chain(votes)
      .countBy()
      .toPairs()
      .sortBy(1).reverse()
      .map(0)
      .value()[0];
    resolve(tally);
  });

How can I pick a random value IF I have multiple userIds with the same number of votes. At the moment it seems that the smallest ID will always be picked?

5
  • 1
    Instead of picking the first one, check how many there are with the same number of votes and pick a random one amongst them. Commented Nov 4, 2017 at 16:27
  • Why the needless use of a promise? Commented Nov 4, 2017 at 16:29
  • Using .then() somewhere else. Commented Nov 4, 2017 at 16:32
  • But why ? Code shown is synchronous Commented Nov 4, 2017 at 16:34
  • Was getting an error, the promise fixed it, Commented Nov 4, 2017 at 16:37

2 Answers 2

3

Based on your question, I believe your object is as follow, I have used the array method sort to sort the array in descending and picked up the first element. Hope this helps.

let players = [{
                 "userId":1,
                 "currentVotes":2220
               },
               {
                 "userId":2,
                 "currentVotes":383830
               },
               {
                 "userId":3,
                 "currentVotes":6894740
               },
               {
                 "userId":4,
                 "currentVotes":6894740
               },
               {
                 "userId":5,
                 "currentVotes":1
               }
              ];


function getHighestVoteByRandowm(players){

    let arrSameVotes = [];

    let i = 0;
    do{
        arrSameVotes.push(players[i]);
        temp = players[i].currentVotes;
        i++;
    }while(players[i].currentVotes == temp);

    let rndTill = arrSameVotes.length - 1;
    let rndVal = Math.round(Math.random() * rndTill);

    return arrSameVotes[rndVal];

}

function sortVotes(a,b){
    return b.currentVotes - a.currentVotes;
}

let highestVotesPlayer = getHighestVoteByRandowm(players.sort(sortVotes));

console.log(highestVotesPlayer);
Sign up to request clarification or add additional context in comments.

1 Comment

check this out, i have edited the code to get the random value if the votes are same.
1

You could use _.shuffle and _.first. Maybe something like:

_.chain(votes).countBy()
  .groupBy() // added this to get a list of all the ones that have matching vote counts
  .toPairs()
  .sortBy(0)
  .reverse()
  .first() // most votes
  .last() // get the user ids
  .shuffle() // randomize order
  .first() // get whatever is first
  .value()

Comments

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.