2018-10-10 22:03:03 +00:00
|
|
|
---
|
|
|
|
id: 5664820f61c48e80c9fa476c
|
2021-03-15 03:20:39 +00:00
|
|
|
title: 高尔夫代码
|
2018-10-10 22:03:03 +00:00
|
|
|
challengeType: 1
|
2020-04-29 10:29:13 +00:00
|
|
|
videoUrl: 'https://scrimba.com/c/c9ykNUR'
|
|
|
|
forumTopicId: 18195
|
2021-01-13 02:31:00 +00:00
|
|
|
dashedName: golf-code
|
2018-10-10 22:03:03 +00:00
|
|
|
---
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
# --description--
|
2018-10-10 22:03:03 +00:00
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
在[高尔夫](https://en.wikipedia.org/wiki/Golf)游戏中,每个洞都有自己的标准杆数 `par`,代表着把球打进洞所挥杆的平均次数 `strokes`。 根据你把球打进洞所挥杆的次数 `strokes` 高于或低于 `par` 多少,有一个不同的昵称(代表打高尔夫球的水平)。
|
2020-12-16 07:37:30 +00:00
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
函数将会传送两个参数,`par` 和 `strokes`。 根据下表返回正确的字符串。下表列出不同挥杆次数(从高到低)对应的字符串。
|
2020-12-16 07:37:30 +00:00
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
<table class='table table-striped'><thead><tr><th>挥杆次数</th><th>返回字符串</th></tr></thead><tbody><tr><td>1</td><td>"Hole-in-one!"</td></tr><tr><td><= par - 2</td><td>"Eagle"</td></tr><tr><td>par - 1</td><td>"Birdie"</td></tr><tr><td>par</td><td>"Par"</td></tr><tr><td>par + 1</td><td>"Bogey"</td></tr><tr><td>par + 2</td><td>"Double Bogey"</td></tr><tr><td>>= par + 3</td><td>"Go Home!"</td></tr></tbody></table>
|
2020-12-16 07:37:30 +00:00
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
`par` 和 `strokes` 必须是数字而且是正数。 题目已定义字符串的数组,便于你操作。
|
2020-12-16 07:37:30 +00:00
|
|
|
|
|
|
|
# --hints--
|
2018-10-10 22:03:03 +00:00
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
`golfScore(4, 1)` 应该返回字符串 `Hole-in-one!`
|
2018-10-10 22:03:03 +00:00
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
```js
|
|
|
|
assert(golfScore(4, 1) === 'Hole-in-one!');
|
|
|
|
```
|
2018-10-10 22:03:03 +00:00
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
`golfScore(4, 2)` 应该返回字符串 `Eagle`
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
|
|
```js
|
2020-12-16 07:37:30 +00:00
|
|
|
assert(golfScore(4, 2) === 'Eagle');
|
|
|
|
```
|
2018-10-10 22:03:03 +00:00
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
`golfScore(5, 2)` 应该返回字符串 `Eagle`
|
2018-10-10 22:03:03 +00:00
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
```js
|
|
|
|
assert(golfScore(5, 2) === 'Eagle');
|
|
|
|
```
|
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
`golfScore(4, 3)` 应该返回字符串 `Birdie`
|
2018-10-10 22:03:03 +00:00
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
```js
|
|
|
|
assert(golfScore(4, 3) === 'Birdie');
|
2018-10-10 22:03:03 +00:00
|
|
|
```
|
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
`golfScore(4, 4)` 应该返回字符串 `Par`
|
2018-10-10 22:03:03 +00:00
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
```js
|
|
|
|
assert(golfScore(4, 4) === 'Par');
|
|
|
|
```
|
2018-10-10 22:03:03 +00:00
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
`golfScore(1, 1)` 应该返回字符串 `Hole-in-one!`
|
2018-10-10 22:03:03 +00:00
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
```js
|
|
|
|
assert(golfScore(1, 1) === 'Hole-in-one!');
|
|
|
|
```
|
2018-10-10 22:03:03 +00:00
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
`golfScore(5, 5)` 应该返回字符串 `Par`
|
2020-12-16 07:37:30 +00:00
|
|
|
|
|
|
|
```js
|
|
|
|
assert(golfScore(5, 5) === 'Par');
|
|
|
|
```
|
2018-10-10 22:03:03 +00:00
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
`golfScore(4, 5)` 应该返回字符串 `Bogey`
|
2020-04-29 10:29:13 +00:00
|
|
|
|
2018-10-10 22:03:03 +00:00
|
|
|
```js
|
2020-12-16 07:37:30 +00:00
|
|
|
assert(golfScore(4, 5) === 'Bogey');
|
|
|
|
```
|
2020-04-29 10:29:13 +00:00
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
`golfScore(4, 6)` 应该返回字符串 `Double Bogey`
|
2020-04-29 10:29:13 +00:00
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
```js
|
|
|
|
assert(golfScore(4, 6) === 'Double Bogey');
|
|
|
|
```
|
2020-04-29 10:29:13 +00:00
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
`golfScore(4, 7)` 应该返回字符串 `Go Home!`
|
2020-04-29 10:29:13 +00:00
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
```js
|
|
|
|
assert(golfScore(4, 7) === 'Go Home!');
|
|
|
|
```
|
2020-04-29 10:29:13 +00:00
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
`golfScore(5, 9)` 应该返回字符串 `Go Home!`
|
2020-04-29 10:29:13 +00:00
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
```js
|
|
|
|
assert(golfScore(5, 9) === 'Go Home!');
|
2018-10-10 22:03:03 +00:00
|
|
|
```
|
2020-04-29 10:29:13 +00:00
|
|
|
|
2021-01-13 02:31:00 +00:00
|
|
|
# --seed--
|
|
|
|
|
|
|
|
## --seed-contents--
|
|
|
|
|
|
|
|
```js
|
|
|
|
var names = ["Hole-in-one!", "Eagle", "Birdie", "Par", "Bogey", "Double Bogey", "Go Home!"];
|
|
|
|
function golfScore(par, strokes) {
|
|
|
|
// Only change code below this line
|
|
|
|
|
|
|
|
|
|
|
|
return "Change Me";
|
|
|
|
// Only change code above this line
|
|
|
|
}
|
|
|
|
|
|
|
|
golfScore(5, 4);
|
|
|
|
```
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
# --solutions--
|
|
|
|
|
2021-01-13 02:31:00 +00:00
|
|
|
```js
|
|
|
|
function golfScore(par, strokes) {
|
|
|
|
if (strokes === 1) {
|
|
|
|
return "Hole-in-one!";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strokes <= par - 2) {
|
|
|
|
return "Eagle";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strokes === par - 1) {
|
|
|
|
return "Birdie";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strokes === par) {
|
|
|
|
return "Par";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strokes === par + 1) {
|
|
|
|
return "Bogey";
|
|
|
|
}
|
|
|
|
|
|
|
|
if(strokes === par + 2) {
|
|
|
|
return "Double Bogey";
|
|
|
|
}
|
|
|
|
|
|
|
|
return "Go Home!";
|
|
|
|
}
|
|
|
|
```
|