Records:
Golf Code
In the game of Golf, each hole has a par, meaning, the average number of strokes a golfer is expected to make in order to sink the ball in the hole to complete the play. Depending on how far above or below par your strokes are, there is a different nickname.
Your function will be passed par and strokes arguments. Return the correct string according to this table which lists the strokes in order of priority; top (highest) to bottom (lowest):
| Strokes | Return |
|---|---|
| 1 | “Hole-in-one!” |
| <= par - 2 | “Eagle” |
| par - 1 | “Birdie” |
| par | “Par” |
| par + 1 | “Bogey” |
| par + 2 | “Double Bogey” |
| >= par + 3 | “Go Home!” |
par and strokes will always be numeric and positive. We have added an array of all the names for your convenience.
Instance Code
const names = ["Hole-in-one!", "Eagle", "Birdie", "Par", "Bogey", "Double Bogey", "Go Home!"];
function golfScore(par, strokes) {
const length = names.length-1;
const arerage = length / 2;
if(par == strokes) {
if(strokes <= 1){
return names[0];
}
return names[arerage];
}else if(par > strokes && par < length){
return names[strokes - 1];
}else {
const index = strokes - par + arerage;
return index < length ? names[index] : names[length];
}
}
golfScore(4, 1) // should return the string Hole-in-one!
golfScore(4, 2) // should return the string Eagle
golfScore(5, 2) // should return the string Eagle
golfScore(4, 3) // should return the string Birdie
golfScore(4, 4) // should return the string Par
golfScore(1, 1) // should return the string Hole-in-one!
golfScore(5, 5) // should return the string Par
golfScore(4, 5) // should return the string Bogey
golfScore(4, 6) // should return the string Double Bogey
golfScore(4, 7) // should return the string Go Home!
golfScore(5, 9) // should return the string Go Home!
该文章介绍了一个计算高尔夫球洞得分的函数,根据球手相对于标准杆(Par)的击球数返回不同的得分名称,如Hole-in-one!,Eagle,Birdie等。函数基于输入的Par和Strokes参数进行判断并返回相应的得分类型。
2117

被折叠的 条评论
为什么被折叠?



