Skip to content

Commit

Permalink
Added another file exercise002.js in directory Hacktoberfest-2021/Fir…
Browse files Browse the repository at this point in the history
…dausRazali/js-exercise
  • Loading branch information
matyod committed Sep 8, 2023
1 parent 42a7a63 commit 6059274
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions Hacktoberfest-2021/FirdausRazali/js-exercise/exercise002.js
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));

0 comments on commit 6059274

Please sign in to comment.