0

So the current javascript program I'm working on is a bidding system where users place their bids and the highest one wins. However I'm stuck on the problem of having duplicate bids and how to deal with them. My idea was to take the duplicate bids and put them in a separate array and random between them to declare the winner. So for example I've placed the names of the bidders and their bid into a multidimensional array:

bidarray = [ ['Sarah', 55], ['John', 55], ['Joe', 20], ['David', 30], ['George', 55], ['Yogi', 10] ];

I want to extract the current highest bids that are equal and place them into another array in order to iterate through them and random for the winner. So the duplicate array would be:

duparray = [ ['Sarah, 55], ['John', 55], ['George', 55] ];

Is there a method to do this? My programming skills are beginner level so I'm not sure where to begin. Thank you for any help!

madruk20
  • 31
  • 1
  • Find the top bid, then use `filter` to get only the bids that have that top bid value. The first part is a loop where you remember the highest bid you've seen. See the linked question's answers for how to do the second part. – T.J. Crowder May 09 '20 at 07:50
  • Just in case, here is the code. This question is closed so I posted it here. function getHighestBidder(array) { let bidderValue; let biggestBidder = 0; let returnArray = []; for (a of array) { bidderValue = a[1]; if (bidderValue > biggestBidder) { biggestBidder = bidderValue; } } for (a of array) { if (a[1] == biggestBidder) { returnArray.push(a); } } return returnArray; } – Crupeng May 09 '20 at 07:58
  • getHighestBids will first calculate the highest bid value and then use it to filter the bid array ```function getHighestBids ( bidarray ) { const max = Math.max.apply (null, bidarray.map ( bid => bid[1]) ); return bidarray.filter ( bid => bid[1] === max ); }``` – shreyas888 May 09 '20 at 14:24
  • Thank you!! You guys have been a great help <3 – madruk20 May 09 '20 at 22:02

0 Answers0