Golf Code Challenge

I am super stuck, can anyone take a gander at my code?

// 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
}

Also feel free to show me a better way to post my code on the forum, LOL.
major noobie here.

1 Like

I think it is because of the return "Change Me"; you have at the end there.
My solution was as follows:

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

Seems similar… Hope this helps. Happy coding!

1 Like

To format, you can wrap code in back ticks ( ` ) to format it for single-line or inline items. If you want multi-line code, you can indent by four spaces without the backticks:

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

*Also, it could be helpful to post the errors you are getting or more of a description on the specific problem you are having to the forum so people can better help you :slight_smile:

2 Likes

Actually, @JesseNuese your problem is even simpler than forgetting to delete the return “Change me” line: the FCC validator is expecting the hole in one line to read “Hole-in-one!” you left out the exclamation point. just changing that (even leaving in the extraneous return statement since the code never gets to it anyhow) will make your code pass.

4 Likes

Wow, I am devastated lol. Thanks you guys, all of you, for your help.

1 Like

Good point! I’ll keep that in mind next time! Cheers.

In my experience, it’s often the simple typos that get you. And since you aren’t necessarily looking for them they seem the hardest to find.

1 Like

This is so True, I spent whole hour on this. It is better to look for help right away!

1 Like

I’m so glad I read this, looked back at my work and saw the typo “par” instead of “Par”. It was super frustrating up until then as everything was working but wouldn’t pass!

1 Like

is strict equality required here? I got past the challenge with just ==

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
}

Thank you! I also forgot to type ! in the end and I didn’t understand what was wrong because I got Hole-in-one message in log =)

The last line can be written with only an else statement, since it does not matter how bad your strokes are and we fulfill the “DRY” coding part.

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);

**

IS this CODE STUPID?

works fine :smiley:

**


function golfScore(par, strokes) {
  // Only change code below this line
  if (par == 4 && strokes == 1){
    return "Hole-in-one!";
  }
   else if (par == 4 && strokes == 2){
     return "Eagle";
   }
   else if (par == 5 && strokes == 2){
     return "Eagle";
   }
   else if (par == 4 && strokes == 3){
     return "Birdie";
   }
   else if (par == 4 && strokes == 4){
     return "Par";
   }
   else if (par == 1 && strokes == 1){
     return "Hole-in-one!";
   }
   else if (par == 5 && strokes == 5){
     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 == 9){
     return "Go Home!";
   }

  // Only change code above this line
}

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