2018-09-30 23:01:58 +01:00
---
id: 5664820f61c48e80c9fa476c
title: Golf Code
challengeType: 1
---
## 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:
- text: '< code > golfScore(4, 1)</ code > should return "Hole-in-one!"'
2018-10-08 01:01:53 +01:00
testString: 'assert(golfScore(4, 1) === "Hole-in-one!", "< code > golfScore(4, 1)< / code > should return "Hole-in-one!"");'
2018-10-04 14:37:37 +01:00
- text: '< code > golfScore(4, 2)</ code > should return "Eagle"'
2018-10-08 01:01:53 +01:00
testString: 'assert(golfScore(4, 2) === "Eagle", "< code > golfScore(4, 2)< / code > should return "Eagle"");'
2018-10-04 14:37:37 +01:00
- text: '< code > golfScore(5, 2)</ code > should return "Eagle"'
2018-10-08 01:01:53 +01:00
testString: 'assert(golfScore(5, 2) === "Eagle", "< code > golfScore(5, 2)< / code > should return "Eagle"");'
2018-10-04 14:37:37 +01:00
- text: '< code > golfScore(4, 3)</ code > should return "Birdie"'
2018-10-08 01:01:53 +01:00
testString: 'assert(golfScore(4, 3) === "Birdie", "< code > golfScore(4, 3)< / code > should return "Birdie"");'
2018-10-04 14:37:37 +01:00
- text: '< code > golfScore(4, 4)</ code > should return "Par"'
2018-10-08 01:01:53 +01:00
testString: 'assert(golfScore(4, 4) === "Par", "< code > golfScore(4, 4)< / code > should return "Par"");'
2018-10-04 14:37:37 +01:00
- text: '< code > golfScore(1, 1)</ code > should return "Hole-in-one!"'
2018-10-08 01:01:53 +01:00
testString: 'assert(golfScore(1, 1) === "Hole-in-one!", "< code > golfScore(1, 1)< / code > should return "Hole-in-one!"");'
2018-10-04 14:37:37 +01:00
- text: '< code > golfScore(5, 5)</ code > should return "Par"'
2018-10-08 01:01:53 +01:00
testString: 'assert(golfScore(5, 5) === "Par", "< code > golfScore(5, 5)< / code > should return "Par"");'
2018-10-04 14:37:37 +01:00
- text: '< code > golfScore(4, 5)</ code > should return "Bogey"'
2018-10-08 01:01:53 +01:00
testString: 'assert(golfScore(4, 5) === "Bogey", "< code > golfScore(4, 5)< / code > should return "Bogey"");'
2018-10-04 14:37:37 +01:00
- text: '< code > golfScore(4, 6)</ code > should return "Double Bogey"'
2018-10-08 01:01:53 +01:00
testString: 'assert(golfScore(4, 6) === "Double Bogey", "< code > golfScore(4, 6)< / code > should return "Double Bogey"");'
2018-10-04 14:37:37 +01:00
- text: '< code > golfScore(4, 7)</ code > should return "Go Home!"'
2018-10-08 01:01:53 +01:00
testString: 'assert(golfScore(4, 7) === "Go Home!", "< code > golfScore(4, 7)< / code > should return "Go Home!"");'
2018-10-04 14:37:37 +01:00
- text: '< code > golfScore(5, 9)</ code > should return "Go Home!"'
2018-10-08 01:01:53 +01:00
testString: 'assert(golfScore(5, 9) === "Go Home!", "< code > golfScore(5, 9)< / code > should return "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
}
// Change these values to test
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 >