DEV Community

Discussion on: Daily Challenge #41 - Greed is Good

Collapse
 
alvaromontoro profile image
Alvaro Montoro

JavaScript

const greedy = arr => {
  let points = 0;
  const dices = { 1:0, 2:0, 3:0, 4:0, 5:0, 6:0 };
  arr.forEach(el => dices[el]++);

  Object.keys(dices).forEach(key => {
    if (dices[key] >= 3) {
      points += key * (key == 1 ? 1000 : 100);
      if (key == 1) points += (dices[key] - 3) * 100;
      if (key == 5) points += (dices[key] - 3) * 50;
    } else {
      if (key == 1) points += dices[key] * 100;
      if (key == 5) points += dices[key] * 50;
    }
  })

  return points;
}

Live demo on CodePen.