# Golf Code Problem

Golf Code Problem
0

#1

So far I’ve got everything on this challenge correct except the last 2 else if’s which is the one for “Double Bogey” and “Go Home!”. Do you know what’s the problem? Here’s what I done

``````function golfScore(par, strokes) {
// Only change code below this line
if (strokes === 1) {
return "Hole-in-one!";
} else if (strokes <= par - 2) {
return "Eagle";
} else if (strokes <= par - 1) {
return "Birdie";
} else if (strokes === par) {
return "Par";
} else if (strokes >= par + 1) {
return "Bogey";
} else if (strokes >= par + 2) {
return "Double Bogey";
} else if (strokes >= par + 3) {
return "Go Home!";
}
// Only change code above this line
}

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

#2

First of all, please indent your code - you’ll thank yourself later.

Secondly, think about the logic. When we get to

``````} else if (strokes >= par + 1) {
``````

it is testing if you are one or more over par. This is still true if you are 2 or 3 or 127 over par so that is evaluated and it never gets past that. You can fix this by reordering your logic. I’ll leave it to you to figure it out. Ask us again if you can’t figure it out.

#3

I guess I could point out that there are other ways to solve it too. Instead of switching to >, you could keep using <. Since you’ve already peeled off the lesser numbers, you would be targeting only the higher ones.

``````} else if (strokes >= par + 1) {
``````

you could have

``````} else if (strokes <= par + 1) {
``````

since we know that all the lower scores have been been taken care of. Extending that logic, it only really needs to be

``````} else if (strokes === par + 1) {
``````

Since we know that that is the only score we really need to target with that evaluation - any lower score has already been returned and any higher score has already been evaluated. Really, the only ones that don’t need equality are the “eagle” and “go home” to catch higher or lower scores. Your logic of > and < works my coincidence that any number that would break it is already filtered out, but is not the best logic. And in theory bad coding practice as I think equality evaluates faster than comparison.

Just some thoughts.

#4

Late reply but thanks I got it figured out