Vukasin
Vukasin

Reputation: 33

Sum only those elements that passes the condition using map

I have two arrays:
teamA = [97, 112, 101]
teamB = [109, 95, 123]

Each element in the array is the score of the specific team in one game. I need to set a condition that will sum only the scores above 100. So for one team the condition is: If the team score is above 100 sum that score with another that is also above 100, and if the sum of that scores is higher than the sum of scores of the other team, that team wins.

I tought that through mapping I can take the scores that are above 100, but I get a new array that has the first element of undefined.

My code example:

const dolphinsGames = [97, 112, 101];
const koalasGames = [109, 95, 123];

const dolphinsScoreAbove = dolphinsGames.map((dolphinsGame) => {
if (dolphinsGame >= 100) {
    return dolphinsGame;
}
});
const koalasScoreAbove = koalasGames.map((koalasGame) => {
if (koalasGame >= 100) {
   return koalasGame;
}
});

The result is: enter image description here

Upvotes: 1

Views: 494

Answers (4)

giggo1604
giggo1604

Reputation: 511

Map always creates an array of same length mapping every single entry. In your case the mapping function implicitly returns undefined in your "else" case.

One way to solve this is to use flatMap:

dolphinGames.flatMap((dolphinGame) => dolphinGame >= 100 ? dolpinGame : [])

EDIT: As discussed in the comments even though this example is working the better options would be a simple filter because we are not actually changing the elements in the mapping function.

Upvotes: 0

Vanmeeganathan P K
Vanmeeganathan P K

Reputation: 211

You can achieve what you are trying for using filter() method. Unlike map(), the filter callback function expects true/false to be returned. Hence, I would modify your program as,

const dolphinsGames = [97, 112, 101];
const koalasGames = [109, 95, 123];

const dolphinsScoreAbove = dolphinsGames.filter((dolphinsGame) => {
  if (dolphinsGame >= 100) {
      return true;
  }
  return false;
});

const koalasScoreAbove = koalasGames.filter((koalasGame) => {
  if (koalasGame >= 100) {
     return true;
  }
  return false;
});

console.log(dolphinsScoreAbove);
console.log(koalasScoreAbove);

Upvotes: 0

Nina Scholz
Nina Scholz

Reputation: 386726

Array#map returns a new array with the same length of the array and takes the result of the callback for every item.

Instead, you could take Array#filter which ilters the array and returns either the item or not, depending on the filter function.

const
    valid = score => score >= 100,
    add = (a, b) => a + b,
    dolphinsGames = [97, 112, 101],
    koalasGames = [109, 95, 123],
    dolphinsScore = dolphinsGames.filter(valid).reduce(add, 0),
    koalasScore = koalasGames.filter(valid).reduce(add, 0);

console.log(dolphinsScore, koalasScore);

if (dolphinsScore === koalasScore) console.log('draw');
else if (dolphinsScore > koalasScore) console.log('dolphins won');
else console.log('koalas won');

Upvotes: 2

KcH
KcH

Reputation: 3502

From your comments is this what you need ? though no map used :(

const dolphinsGames = [97, 112, 101];
const koalasGames = [109, 95, 123];

const average = arr => arr.reduce( ( p, c ) => p + c, 0 ) / arr.length;

const dolphinsAvg = average(dolphinsGames);
const koalasAvg = average(koalasGames);

if(dolphinsAvg > 100 && koalasAvg > 100){
    if(dolphinsAvg === koalasAvg){
    console.log('draw') 
  }
  else if(dolphinsAvg > koalasAvg ){
    console.log('dolphins win')
  }
  else{
   console.log('koalas win')
  }
}
else{
    console.log('Average points is less than 100')
}

Upvotes: 0

Related Questions