mirror of
https://github.com/freeCodeCamp/freeCodeCamp.git
synced 2025-12-19 10:07:46 -05:00
1.6 KiB
1.6 KiB
id, title, challengeType, dashedName
| id | title | challengeType | dashedName |
|---|---|---|---|
| 69373793f5a867f769cde139 | Challenge 154: Par for the Hole | 28 | challenge-154 |
--description--
Given two integers, the par for a golf hole and the number of strokes a golfer took on that hole, return the golfer's score using golf terms.
Return:
"Hole in one!"if it took one stroke."Eagle"if it took two strokes less than par."Birdie"if it took one stroke less than par."Par"if it took the same number of strokes as par."Bogey"if it took one stroke more than par."Double bogey"if took two strokes more than par.
--hints--
golfScore(3, 3) should return "Par".
assert.equal(golfScore(3, 3), "Par");
golfScore(4, 3) should return "Birdie".
assert.equal(golfScore(4, 3), "Birdie");
golfScore(3, 1) should return "Hole in one!".
assert.equal(golfScore(3, 1), "Hole in one!");
golfScore(5, 7) should return "Double bogey".
assert.equal(golfScore(5, 7), "Double bogey");
golfScore(4, 5) should return "Bogey".
assert.equal(golfScore(4, 5), "Bogey");
golfScore(5, 3) should return "Eagle".
assert.equal(golfScore(5, 3), "Eagle");
--seed--
--seed-contents--
function golfScore(par, strokes) {
return par;
}
--solutions--
function golfScore(par, strokes) {
if (strokes === 1) return "Hole in one!";
const diff = strokes - par;
if (diff === -2) return "Eagle";
if (diff === -1) return "Birdie";
if (diff === 0) return "Par";
if (diff === 1) return "Bogey";
if (diff === 2) return "Double bogey";
}