Journal Archive

function score( dice ) {
  let score = 0;
  // count occurrence of each number and store as object
  const diceCount = dice.reduce((obj,value) => {
    obj[value] ? obj[value]++ : obj[value] = 1;
    return obj;
  }, {});
  // loop through each number that occurred and update score
  for (let number in diceCount) {
    switch(number) {
        case "1":
          diceCount[number] >= 3 ? 
            score += 1000 + (diceCount[number] % 3 * 100): 
            score += diceCount[number] * 100;
          break;
        case "2":
          diceCount[number] >= 3 ? score += 200: score += 0;
          break;
        case "3":
          diceCount[number] >= 3 ? score += 300: score += 0;
          break;
        case "4":
          diceCount[number] >= 3 ? score += 400: score += 0;
          break;
        case "5":
          diceCount[number] >= 3 ? 
            score += 500 + (diceCount[number] % 3 * 50): 
            score += diceCount[number] * 50;
          break;
        case "6":
          diceCount[number] >= 3 ? score += 600: score += 0;
          break;
        default:
          break;
    }
  }
  return score;
}

Day 9: Solving one of the Kata on CodeWars

Greed is Good 5 kyu

Greed is a dice game played with five six-sided dice. Your mission, should you choose to accept it, is to score a throw according to these rules. You will always be given an array with five six-sided dice values.

Three 1's => 1000 points
Three 6's => 600 points
Three 5's => 500 points
Three 4's => 400 points
Three 3's => 300 points
Three 2's => 200 points
One 1 => 100 points
One 5 => 50 point

A single die can only be counted once in each roll. For example, a given "5" can only count as part of a triplet (contributing to the 500 points) or as a single 50 points, but not both in the same roll.