Felipe
Felipe

Reputation: 462

nodejs javascript find with condition

Hello I have a queue of players and wanted to get the first player that met one of my two conditions, but I am unsure how to do that.

First I get the value of a player (mmr) and wanted to get one (only one) player from the queue that had 5% more or less of this value

My condition I think is this to get 5% more or less:

  const condition = (5/100)*playerOne.mmr + playerOne.mmr
  const condition2 = (5/100)*playerOne.mmr - playerOne.mmr

my function:

makeMatch(playerOne) {
  // make matched players
  const condition = (5/100)*playerOne.mmr + playerOne.mmr
  const condition2 = (5/100)*playerOne.mmr - playerOne.mmr
  const player = playerOne;
  const matchedPlayer = this.players.find();
  // remove matched players from this.players
  this.removePlayers(matchedPlayers);
  // return new Match with matched players
  return new Match(matchedPlayers);
}

I have questions as I would get 5% more or less within my find

Upvotes: 0

Views: 898

Answers (2)

Shimmy568
Shimmy568

Reputation: 503

Check out the documentation for the find function it details this use case. Here's the line that you would want for the desired result.

const matchedPlayer = this.players.find((playerTwo) => playerTwo.mmr > condition || playerTwo.mmr > condition2);

Upvotes: 1

Enrico Cortinovis
Enrico Cortinovis

Reputation: 861

You can try something like this:

if (player.condition1 > condition1*0.95 && player.condition1 < condition1*1.05) {
   // then you keep the player
} else {
   // else you remove it from the list
}

Also, I would highly reccomend you to give better identifiers to the variables. Instead of condition1, something like age, height, weight.

Upvotes: 0

Related Questions