Skip to content

Instantly share code, notes, and snippets.

@javi-aire
Created March 3, 2020 05:47
Show Gist options
  • Save javi-aire/3c405047f9c71fc7902feb3776080d94 to your computer and use it in GitHub Desktop.
Save javi-aire/3c405047f9c71fc7902feb3776080d94 to your computer and use it in GitHub Desktop.
function upperSectionScore(diceResult){
// handle empty array
if(diceResult.length === 0){
return -1;
}
// will need a way to count the frequency of each number
// a variable to store/return the value, and store the largest num freq
let frequency = {
'1': 0,
'2': 0,
'3': 0,
'4': 0,
'5': 0,
'6': 0
},
largestFreqNum = 1,
score = 1;
// TODO:
// 1) loop thru each roll, counting the frequency of each number rolled
// 2) find the largestFreqNum num
// 3) using the largestFreqNum, calculate the value and return the score.
diceResult.forEach((roll, idx) => {
// 1)
frequency[roll] += 1;
let numToCompare = diceResult[idx];
// 2)
if(frequency[largestFreqNum] <= frequency[numToCompare]){
largestFreqNum = numToCompare;
}
});
// 3)
score = largestFreqNum * frequency[largestFreqNum];
return score;
}
console.log(upperSectionScore([2, 3, 5, 5, 6])); // 10
console.log(upperSectionScore([1, 1, 1, 1, 1])); // 5
console.log(upperSectionScore([])); // -1
console.log(upperSectionScore([2, 4, 3, 4, 4])); // 12
console.log(upperSectionScore([6, 6, 6, 6, 6])); // 30
console.log(upperSectionScore([1, 1, 3, 2, 3])); // 6
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment