# Golf Code exercise

Hi guys, I am struggling with this exercise https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-javascript/golf-code. I went back to the basics and move on from here: https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-javascript/introducing-else-if-statements but I am still stuck.

My code is:

``````const 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 "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
}

golfScore(5, 4);

``````

And it only says the ones on top of Bogey are correct :

``````Passed: golfScore(4, 1) should return the string Hole-in-one!
Passed: golfScore(4, 2) should return the string Eagle
Passed: golfScore(5, 2) should return the string Eagle
Passed: golfScore(4, 3) should return the string Birdie
Passed: golfScore(4, 4) should return the string Par
Passed: golfScore(1, 1) should return the string Hole-in-one!
Passed: golfScore(5, 5) should return the string Par
Failed: golfScore(4, 5) should return the string Bogey
Failed: golfScore(4, 6) should return the string Double Bogey
Failed: golfScore(4, 7) should return the string Go Home!
Failed: golfScore(5, 9) should return the string Go Home!
``````

Please, if someone could give me a hink on what I am failing without letting me know the answer I would really apreciatte. Kind regards,
Manuel

Edit:

The thing is, when I change the order of the code :

``````const 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 "Hole-in-one!";
} else if (strokes >= par +3) {
return "Go Home!";
} else if (strokes >= par +2) {
return "Double Bogey";
} else if (strokes >= par +1) {
return "Bogey";
} else if (strokes = par) {
return "Par";
} else if (strokes <= par - 1) {
return "Birdie";
} else if (strokes <= par -2 ) {
return "Eagle";
}
// Only change code above this line
}

golfScore(5, 4);
``````

It is giving me correct the ones on top of “Birdie” as I show here:

``````Passed: golfScore(4, 1) should return the string Hole-in-one!
Failed: golfScore(4, 2) should return the string Eagle
Failed: golfScore(5, 2) should return the string Eagle
Failed: golfScore(4, 3) should return the string Birdie
Passed: golfScore(4, 4) should return the string Par
Passed: golfScore(1, 1) should return the string Hole-in-one!
Passed: golfScore(5, 5) should return the string Par
Passed: golfScore(4, 5) should return the string Bogey
Passed: golfScore(4, 6) should return the string Double Bogey
Passed: golfScore(4, 7) should return the string Go Home!
Passed: golfScore(5, 9) should return the string Go Home!
``````

so It just so stuck, my head is

This is an assignment.
It assigns the value of oar to the variable strokes.

Did you want to do that or did you want to compare the values? (===)

1 Like

I wanted to compare the values I guess, but when I write (=) It seems valid for that else if line. I tried it and change for (===) to compare the values (maybe the ones down the string “Par”) which ones are failing and still saying that It should return the strings “Eagle” and “Birdie”

Edit:

The thing is now @hbar1st, what you say about (===) I changed it with the first code (following the order of the tablet) :

``````const 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 "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
}

golfScore(5, 4);
``````

And now it says this :

``````Passed: golfScore(4, 1) should return the string Hole-in-one!
Passed: golfScore(4, 2) should return the string Eagle
Passed: golfScore(5, 2) should return the string Eagle
Passed: golfScore(4, 3) should return the string Birdie
Passed: golfScore(4, 4) should return the string Par
Passed: golfScore(1, 1) should return the string Hole-in-one!
Passed: golfScore(5, 5) should return the string Par
Passed: golfScore(4, 5) should return the string Bogey
Failed: golfScore(4, 6) should return the string Double Bogey
Failed: golfScore(4, 7) should return the string Go Home!
Failed: golfScore(5, 9) should return the string Go Home!
``````

Now the string “Bogey” is OK. Before it was not (due to the assignment) now as you said I compared it and string “Bogey” is OK. But the ones down “Bogey” still failing… why? I just do not get it

1 Like

The logic is wrong. Reread the exercise to see what is the correct logic for these two.

1 Like

Nothing, I reread the exercise many times and tried many different things and still can’t get the exercise done

This logic dictates that if the number of strokes is greater than or equal to par + 1 then you return ‘Bogey’. So that means that ‘Bogey’ would also be returned if strokes = par + 2, or par + 100 for instance… Do you see what the issue is?

Not really, because now “Bogey” says it is correct. as you can see here :

``````Passed: golfScore(4, 1) should return the string Hole-in-one!
Passed: golfScore(4, 2) should return the string Eagle
Passed: golfScore(5, 2) should return the string Eagle
Passed: golfScore(4, 3) should return the string Birdie
Passed: golfScore(4, 4) should return the string Par
Passed: golfScore(1, 1) should return the string Hole-in-one!
Passed: golfScore(5, 5) should return the string Par
Passed: golfScore(4, 5) should return the string Bogey
Failed: golfScore(4, 6) should return the string Double Bogey
Failed: golfScore(4, 7) should return the string Go Home!
Failed: golfScore(5, 9) should return the string Go Home!
``````

so That is why I applied the same logic for “Double Bogey” and “Go Home!”

at least it should be correct for “Double Bogey” as it follows the same pattern than “Bogey” that is why I do not understand, see the table attached:

``````1	"Hole-in-one!"
<= par - 2	"Eagle"
par - 1	"Birdie"
par	"Par"
par + 1	"Bogey"
par + 2	"Double Bogey"
>= par + 3	"Go Home!"
``````

so the logic for par +1 it supposed to be the same for par +2. only changing the number but it is said is incorrect and it does not return the string.

``````Passed: golfScore(4, 5) should return the string Bogey
Failed: golfScore(4, 6) should return the string Double Bogey
Failed: golfScore(4, 7) should return the string Go Home!
Failed: golfScore(5, 9) should return the string Go Home!
``````

Edit: I am still with the code below:

``````const 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 "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
}

golfScore(5, 4);
``````

which @hbar1st say the logic for the two last are wrong, but I do not why, at least on “Double Bogey” when I am applying the same logic than “Bogey”, only thing I am changing is the number as the table shows

A return statement immediately stops the function call.

1 Like

Sorry Jeremy what do you mean with this? If I do now write return statement then I will not be able to have the strings “Double Bogey” and “Go Home” back right?

Sorry guys but I just so stuck with this exercise. It does not make sense in my head… maybe this is not for me

What happens if strokes === par + 2?

It says the same:

``````  } else if (strokes === par + 2) {
return "Double Bogey";
} else if (strokes >= par + 3) {
return "Go Home!";
}
``````
``````Failed: golfScore(4, 6) should return the string Double Bogey
Failed: golfScore(4, 7) should return the string Go Home!
Failed: golfScore(5, 9) should return the string Go Home!
``````

I also tried :

``````  } else if (strokes === par + 2) {
return "Double Bogey";
} else if (strokes === par + 3) {
return "Go Home!";
``````

and nothing

This is the wrong logic now.
You need to reread the exercise for this one.

I can read your code. I don’t think you understand what that piece is doing.

So, please, tell me in words what will happen if strokes === par + 2

It means strict equality. so if I write that it suposed to be returning false, as the operands are different ( strokes is different from par +2)

All right . I will try. The thing is I gotta go to work and I feel like I want to stay the whole day trying to resolve this let see if in 10 min I can get it done

What does your code do if you pass in arguments where strokes === par + 2? Like if strokes = 3 and par = 1

It will fail as it is not the same.

I tried also to do same calculations with and I did not make sense.

let say this :

``````  } else if (strokes <= par - 1) {
return "Birdie";
``````
``````Passed: golfScore(4, 3) should return the string Birdie
``````

strokes 4 par 3 . 4 <= 3-1 which is 2. Then 4 <= 2 it does not make sense. Anyways it says it is valid. That is why I did not apply the numbers.

I am trying to get you to look at a specific case. ‘It fails’. Ok, but how? What does it do?

1 Like

Sorry It does not fail. It is equal.

strokes === par +2

strokes = 3
par = 1

3 === 2+1 (3)

Which version of your code are you talking about? How are you checking what happens? What message is returned by your function?

It looks like you might be talking about what you think might happen instead of what actually happens.

1 Like