Algorithm/codewars

Greed is Good 답안

Bonita SY 2021. 3. 24. 21:31
728x90

문제)

www.codewars.com/kata/5270d0d18625160ada0000e4/train/javascript

 

Codewars: Achieve mastery through challenge

Codewars is where developers achieve code mastery through challenge. Train on kata in the dojo and reach your highest potential.

www.codewars.com

 

내가 푼 답)

const Dice_Point = {
  '111': 1000,
  '666': 600,
  '555': 500,
  '444': 400,
  '333': 300,
  '222': 200,
  '1': 100,
  '5': 50
};
function score( dice ) {
  var diceValue = dice.sort().join('');
  
  var totalScore = 0;
  while(diceValue !== '') {
    var tmpValue = diceValue.slice(0, 3);
    if (Dice_Point[tmpValue]) {
      totalScore += Dice_Point[tmpValue];
      diceValue = diceValue.replace(tmpValue, '');
    } else {
      tmpValue = diceValue[0];
      if (Dice_Point[tmpValue]) {
        totalScore += Dice_Point[tmpValue];
      }
      diceValue = diceValue.replace(tmpValue, '');
    }
  }
  
  return totalScore;
}

 

테스트 결과)

 

 

 

728x90