-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added another file exercise002.js in directory Hacktoberfest-2021/Fir…
…dausRazali/js-exercise
- Loading branch information
Showing
1 changed file
with
45 additions
and
0 deletions.
There are no files selected for viewing
45 changes: 45 additions & 0 deletions
45
Hacktoberfest-2021/FirdausRazali/js-exercise/exercise002.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
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. | ||
`const names = ["Hole-in-one!", "Eagle", "Birdie", "Par", "Bogey", "Double Bogey", "Go Home!"]; | ||
*/ | ||
|
||
const names=["Hole-in-one!", "Eagle", "Birdie", "Par", "Bogey", "Double Bogey", "Go Home!"]; | ||
|
||
function golfScore(par, strokes){ | ||
if (strokes === 1) { | ||
return names[0]; | ||
} else if (strokes <= par - 2){ | ||
return names[1]; | ||
} else if (strokes === par - 1){ | ||
return names[2]; | ||
} else if (strokes === par){ | ||
return names[3]; | ||
} else if (strokes === par + 1){ | ||
return names[4]; | ||
} else if (strokes === par + 2){ | ||
return names[5]; | ||
} else if (strokes >= par + 3){ | ||
return names[6]; | ||
} | ||
} | ||
|
||
console.log(golfScore(5, 4)); |