freeCodeCamp Challenge Guide: Golf Code

freeCodeCamp Challenge Guide: Golf Code
0

#1

:triangular_flag_on_post: Remember to use Read-Search-Ask if you get stuck. Try to pair program :busts_in_silhouette: and write your own code :pencil:

:checkered_flag: Problem Explanation:

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 a 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. You’ve to 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.

  • Change the code below // Only change code below this line and above // Only change code above this line.
  • Ensure that you’re editing the inside of the golfScore function.
  • You will have to make the function return exactly the same string as shown shown in the table, depending on the value of the parameters par and strokes that are passed to your function.

Relevant Links

:speech_balloon: Hint: 1

+number -number can be used to increase or decrease a parameter in your condition.

try to solve the problem now

:speech_balloon: Hint: 2

You use if / else if chains to return different values in different scenarios.

try to solve the problem now

:speech_balloon: Hint: 3

Control the flow of your function based on the tables order of priority - top (highest) to bottom (lowest) to return matching string values.

try to solve the problem now

Spoiler Alert!

687474703a2f2f7777772e796f75726472756d2e636f6d2f796f75726472756d2f696d616765732f323030372f31302f31302f7265645f7761726e696e675f7369676e5f322e676966.gif

Solution ahead!

:beginner: Basic Code Solution:

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 {
    return "Go Home!";
  }
  // Only change code above this line
}
// Change these values to test
golfScore(5, 4);

Code Explanation:

  • Compare the parameters par and strokes to return appropriate string values.
  • if / else if chain is used for flow control.
  • String “Go Home!” is returned for every condition where strokes is greater than or equal to par + 3.

:clipboard: NOTES FOR CONTRIBUTIONS:

  • :warning: DO NOT add solutions that are similar to any existing solutions. If you think it is similar but better, then try to merge (or replace) the existing similar solution.
  • Add an explanation of your solution.
  • Categorize the solution in one of the following categories — Basic, Intermediate and Advanced. :traffic_light:
  • Please add your username only if you have added any relevant main contents. (:warning: DO NOT remove any existing usernames)

See :point_right: Wiki Challenge Solution Template for reference.


Why is a strict equality operator not plausible for the golf challenge?
#2

I completed it another way, more long winded but shows there is multiple ways to complete probelms!

if (strokes === 1) {
return “Hole-in-one!”;
} else if (strokes === 2) {
return “Eagle”;
} else if (strokes === 3) {
return “Birdie”;
} else if (par === 4 && strokes === 4) {
return “Par”;
} else if (par ===4 && strokes ===5) {
return “Bogey”;
} else if (par ===4 && strokes ===6) {
return “Double Bogey”;
} else if (par >=4 && strokes >=7) {
return “Go Home!”;
} else if (par ===5 && strokes ===5) {
return “Par”;
}


#3

I was able to complete it with a switch (& some help from the community)! Just another way to solve it.

function golfScore(par, strokes) {
// Only change code below this line
var response = “”;
switch(true){
case (strokes === 1):
response = “Hole-in-one!”;
break;
case (strokes <= par -2):
response = “Eagle”;
break;
case (strokes === par -1):
response = “Birdie”;
break;
case (strokes === par):
response = “Par”;
break;
case (strokes === par +1):
response = “Bogey”;
break;
case (strokes === par +2):
response = “Double Bogey”;
break;
case (strokes >= par +3):
response = “Go Home!”;
}

return response;
// Only change code above this line
}


#4

i didn’t know What am I doing at the start. Just was following all the instructions. And got it:

[spoiler]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!”;
}else{
return “change me!”;
}
// Only change code above this line
}

// Change these values to test
golfScore(4, 7);[/spoiler]


#5

Thanks,

Switch Statement did the job for me


#6

I’ve tried, but I don’t know what I’m doing wrong.
These are the errors I received. Help! :frowning_woman:t5:


#8

This is how I did it:

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

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

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


#9

I also came up with a different solution. In this way it doesn’t matter what values you put into golfscore it will always validate. Because it will always resolve back to whatever par is - (par == par). What made me think of it is that a par at a golf course is variable. There are par 4s, 5s, and in rare occasions 6s.

In the real world I would have to resolve the <= comparison operators with the Eagle and Birdie statements, because if you were programming a score board you would want them to resolve to less than 2 strokes under par strictly - because that is what an Eagle is (same with Birdie with 1 under). I tried using strict equals operator === but then it wouldn’t pass and screwed up return of “Hole in one.” Don’t know why. Anyway it will pass no matter which values you put into golfscore the way it stands.

function golfScore(par, strokes) {

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

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

#10

Hi T, I was having the exact issue. I think when I changed the golfscore to (4, 1) it solved that issue. In other words a par 4 with 1 stroke is a hole in one. I say, “I think” because as I was having that issue I came up with another solution and can’t be sure I remember what I was doing before. Check out the code I inserted below. You can enter any value in the golfscore and it will validate and resolve each else if statement properly. Run it in the console and check it out.


#11

Thanks for responding. I will try that to see if it works. T


#12

Hi TlanetteRose,

I was having the same exact problem. I tried a couple different things and finally saw it! You and I CAPITALIZED the “O” in the word “One!”. It should be “one!”. change that and you will be allowed to proceed.


#13

Here is my solution .
function golfScore(par, strokes) {
// Only change code below this line

if((strokes == 1)){
return “Hole-in-one!”;
}
else if((strokes == 2)){
return “Eagle”;
}
else if((strokes == 3)){
return “Birdie”;
}
else if(((strokes == 4) || (strokes == 5)) && (strokes == par)){
return “Par”;
}
else if((strokes == 5) && (strokes != par)){
return “Bogey”;
}
else if((strokes == 6)){
return “Double Bogey”;
}
else{
return “Go Home!”;
}
return “Change Me”;
// Only change code above this line
}

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


#14

Thank you! That was it. :raising_hand_woman:t6: