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
반응형

'Algorithm > codewars' 카테고리의 다른 글

Permutations 답안  (0) 2021.03.29
Recover a secret string from random triplets 답안  (0) 2021.03.29
Moving Zeros To The End 답안  (0) 2021.03.24
Where my anagrams at? 답안  (0) 2021.03.24
Equal Sides Of An Array 답안  (0) 2021.03.24