Basic JavaScript: Golf Code Help

Hello I’m stuck on the Golf Code part of this challenge. I had the first half down put after I get done with the par I got stuck. I feel like I’m getting confused by the golf concept as well here. Here is my code

``````var names = ["Hole-in-one!", "Eagle", "Birdie", "Par", "Bogey", "Double Bogey", "Go Home!"];
function golfScore(par, strokes) {
// Only change code below this line
if (par >= 1 && strokes === 1 ){
return names[0];
} else if(par >= 2 && strokes === 2 ) {
return names[1];
} else if (par >= 3 && strokes === 3){
return names [2];
} else if (par >= 4 && strokes >= 4){
return names[3];
} else if (par >= 4 && strokes  ){
return names[4];
} else if (par >= 4  )

return "Change Me";
// Only change code above this line
}

// Change these values to test
golfScore(4, 5);
``````

In golf, each hole is given a number of strokes it would take most good players to get the ball in the hole, this is called the holes’ `par` - so for example, the longer holes in golf are a par 5, that means if you get the ball in the hole in 5 shots, you got a par on that hole - if you made it in 4 shots (1 less than par), you got a birdie - 3 shots (2 less than par) = eagle - and going the other direction, if you made it in 6 shots (1 more than par), it’s a bogey, 7 = double bogey… so the label given to the score for the hole is the deviation from par, if the hole was a par 4, then finishing it in 3 shots would be a birdie - so what you want to do is make your tests check how far away from par the strokes are, maybe something like…
`if(par + 1 === strokes ) { return "bogey" }` — hope that helps

2 Likes

Thanks I think this helps!

it worked for me! I realized that the task asks for a couple * pairs *, for example: golfScore (4, 2) should return “Eagle” &&
golfScore (5, 2) should return “Eagle”, there should be one logic capable of processing the input pair * pair * (4, 2) && (5, 2). And get the names by index from the array, for example, return names [0];

var names = [“Hole-in-one!”, “Eagle”, “Birdie”, “Par”, “Bogey”, “Double Bogey”, “Go Home!”];
function golfScore(par, strokes) {
// Only change code below this line
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];
}
// Only change code above this line
}

// Change these values to test
golfScore(4, 1);
golfScore(4, 2);
golfScore(5, 2);
golfScore(4, 3);
golfScore(4, 4);
golfScore(1, 1);
golfScore(5, 5);
golfScore(4, 5);
golfScore(4, 6);
golfScore(4, 7);
golfScore(5, 9);