Help with Golf Challenge

why doesnt this work

      if(strokes==2){ return "Eagle";
} else if(strokes==1){ return "Hole-in-one!"; 
} else if(par==strokes){ return "par";
} else if(par==strokes-1){ return "Birdie";
} else if(par==strokes+1){ return "Bogey";
} else if(par==strokes+2){ return "Double Bogey";
} else if(par>strokes+2){ return "Go Home!";
}

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

are you sure this is the condition? try looking at the table again

Also, you have your conditions a little bit backwards.

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!”

These conditions on the left are values that strokes must match.

It could be i didn’t understand golf. I thought par 5 strokes 2 and par 4 strokes 2 are both Eagle.
golfScore(4, 2) should return “Eagle”
golfScore(5, 2) should return “Eagle”

anyways the errors it was giving me was on stuff that we agree should have worked

What this says, in words is “if par is one less than strokes, then you have a Birdie”.

That does not match the table:

the table happens to contradict the tests

i switched it to else if(strokes==par-1){ return "Birdie";

// running tests

golfScore(4, 4)

should return “Par”

golfScore(5, 5)

should return “Par”

golfScore(4, 5)

should return “Bogey”

golfScore(4, 6)

should return “Double Bogey”

golfScore(4, 7)

should return “Go Home!”

golfScore(5, 9)

should return “Go Home!” // tests completed

Did you make that same change for all conditions? What is your current code?

      if(strokes==2){ return "Eagle";
} else if(strokes==1){ return "Hole-in-one!"; 
} else if(par==strokes){ return "par";
} else if(strokes==par-1){ return "Birdie";
} else if(strokes==par+1){ return "Bogey";
} else if(strokes==par+2){ return "Double Bogey";
} else if(strokes>par+2){ return "Go Home!";
}

error

golfScore(4, 4)

should return “Par”

golfScore(5, 5)

should return “Par”

should there be ===

par is not quite Par

1 Like