2018-09-30 23:01:58 +01:00
---
id: 5664820f61c48e80c9fa476c
title: Golf Code
challengeType: 1
2020-05-21 17:31:25 +02:00
isHidden: false
2019-02-14 12:24:02 -05:00
videoUrl: 'https://scrimba.com/c/c9ykNUR'
2019-07-31 11:32:23 -07:00
forumTopicId: 18195
2018-09-30 23:01:58 +01:00
---
## Description
<section id='description'>
In the game of <a href="https://en.wikipedia.org/wiki/Golf" target="_blank">golf</a> each hole has a <code>par</code> meaning the average number of <code>strokes</code> a golfer is expected to make in order to sink the ball in a hole to complete the play. Depending on how far above or below <code>par</code> your <code>strokes</code> are, there is a different nickname.
Your function will be passed <code>par</code> and <code>strokes</code> arguments. Return the correct string according to this table which lists the strokes in order of priority; top (highest) to bottom (lowest):
<table class="table table-striped"><thead><tr><th>Strokes</th><th>Return</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>
<code>par</code> and <code>strokes</code> will always be numeric and positive. We have added an array of all the names for your convenience.
</section>
## Instructions
<section id='instructions'>
</section>
## Tests
<section id='tests'>
```yml
2018-10-04 14:37:37 +01:00
tests:
2018-10-20 21:02:47 +03:00
- text: <code>golfScore(4, 1)</code> should return "Hole-in-one!"
2019-07-13 00:07:53 -07:00
testString: assert(golfScore(4, 1) === "Hole-in-one!");
2018-10-20 21:02:47 +03:00
- text: <code>golfScore(4, 2)</code> should return "Eagle"
2019-07-13 00:07:53 -07:00
testString: assert(golfScore(4, 2) === "Eagle");
2018-10-20 21:02:47 +03:00
- text: <code>golfScore(5, 2)</code> should return "Eagle"
2019-07-13 00:07:53 -07:00
testString: assert(golfScore(5, 2) === "Eagle");
2018-10-20 21:02:47 +03:00
- text: <code>golfScore(4, 3)</code> should return "Birdie"
2019-07-13 00:07:53 -07:00
testString: assert(golfScore(4, 3) === "Birdie");
2018-10-20 21:02:47 +03:00
- text: <code>golfScore(4, 4)</code> should return "Par"
2019-07-13 00:07:53 -07:00
testString: assert(golfScore(4, 4) === "Par");
2018-10-20 21:02:47 +03:00
- text: <code>golfScore(1, 1)</code> should return "Hole-in-one!"
2019-07-13 00:07:53 -07:00
testString: assert(golfScore(1, 1) === "Hole-in-one!");
2018-10-20 21:02:47 +03:00
- text: <code>golfScore(5, 5)</code> should return "Par"
2019-07-13 00:07:53 -07:00
testString: assert(golfScore(5, 5) === "Par");
2018-10-20 21:02:47 +03:00
- text: <code>golfScore(4, 5)</code> should return "Bogey"
2019-07-13 00:07:53 -07:00
testString: assert(golfScore(4, 5) === "Bogey");
2018-10-20 21:02:47 +03:00
- text: <code>golfScore(4, 6)</code> should return "Double Bogey"
2019-07-13 00:07:53 -07:00
testString: assert(golfScore(4, 6) === "Double Bogey");
2018-10-20 21:02:47 +03:00
- text: <code>golfScore(4, 7)</code> should return "Go Home!"
2019-07-13 00:07:53 -07:00
testString: assert(golfScore(4, 7) === "Go Home!");
2018-10-20 21:02:47 +03:00
- text: <code>golfScore(5, 9)</code> should return "Go Home!"
2019-07-13 00:07:53 -07:00
testString: assert(golfScore(5, 9) === "Go Home!");
2018-09-30 23:01:58 +01:00
```
</section>
## Challenge Seed
<section id='challengeSeed'>
<div id='js-seed'>
```js
var names = ["Hole-in-one!", "Eagle", "Birdie", "Par", "Bogey", "Double Bogey", "Go Home!"];
function golfScore(par, strokes) {
// Only change code below this line
2018-10-08 01:01:53 +01:00
2018-09-30 23:01:58 +01:00
return "Change Me";
// Only change code above this line
}
golfScore(5, 4);
```
</div>
</section>
## Solution
<section id='solution'>
```js
function golfScore(par, strokes) {
if (strokes === 1) {
return "Hole-in-one!";
}
2018-10-08 01:01:53 +01:00
2018-09-30 23:01:58 +01:00
if (strokes <= par - 2) {
return "Eagle";
}
2018-10-08 01:01:53 +01:00
2018-09-30 23:01:58 +01:00
if (strokes === par - 1) {
return "Birdie";
}
2018-10-08 01:01:53 +01:00
2018-09-30 23:01:58 +01:00
if (strokes === par) {
return "Par";
}
2018-10-08 01:01:53 +01:00
2018-09-30 23:01:58 +01:00
if (strokes === par + 1) {
return "Bogey";
}
2018-10-08 01:01:53 +01:00
2018-09-30 23:01:58 +01:00
if(strokes === par + 2) {
return "Double Bogey";
}
2018-10-08 01:01:53 +01:00
2018-09-30 23:01:58 +01:00
return "Go Home!";
}
```
</section>