Throwing Darts ( 6 kyu )
문제
You’ve just recently been hired to calculate scores for a Dart Board game!
Scoring specifications:
0 points - radius above 10
5 points - radius between 5 and 10 inclusive
10 points - radius less than 5
If all radiuses are less than 5, award 100 BONUS POINTS!
Write a function that accepts an array of radiuses (can be integers and/or floats), and returns a total score using the above specification.
An empty array should return 0.
Examples:
scoreThrows( [1, 5, 11] ) => 15
scoreThrows( [15, 20, 30] ) => 0
scoreThrows( [1, 2, 3, 4] ) => 140
다트 점수내기, 4발 던져서 1,2,3,4에 넣으면 100점추가
풀이
function scoreThrows(radiuses){
let point = [], result = 0;
for ( let i of radiuses ) {
if ( i < 5 ) {
result += 10;
point.push(i)
} else if ( i >=5 && i <= 10 ) {
result += 5;
}
}
if ( [...new Set(point)].length === 4 && radiuses.length === 4 ) result += 100
return result;
}
케이스를 많이 나눠 생각했는데 테스트케이스가 얼마 없어서 의미가 없어짐
다른 사람의 풀이
function scoreThrows(a){
var result = a.reduce(function(p,c){
return p + (c <5 ? 10 : c > 10 ? 0 : 5);
},0) ;
return result/a.length===10?result+100:result;
}
간단하게 푼 풀이