Error (minor) in "Golf code" tutorial

My code is OK, but the test shows error.

If you have a question about a specific challenge as it relates to your written code for that challenge and need some help, click the Ask for Help button located on the challenge (it looks like a question mark). This button only appears if you have tried to submit an answer at least three times.

The Ask for Help button will create a new topic with all code you have written and include a link to the challenge also. You will still be able to ask any questions in the post before submitting it to the forum.

Thank you.

1 Like

This code works, but the test isn’t OK.

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 <= 0 || par <= 0 ){
    return "Wrong input!"
  }  
else 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"
}
  return "Go Home";
  // Only change code above this line
}
console.log(golfScore(4, 0));
console.log(golfScore(4, 7));
console.log(golfScore(5, 9));

Found the bug!

Typo. You aren’t returning the correct string.

Note - ā€˜my code is perfect and you have to be wrong’ isn’t usually a helpful approach to take when debugging. I get a lot more success with ā€œsomething isn’t working correctly hereā€. There’s always a bug somewhere, and I have more success when I assume I’ve made the mistake.

Hi.
I did never beleive that my code was perfect, but good enough according to the lesson instructions. The feedback by testing weren’t very helpful because console logging of function calls with the ā€œerrorā€ values returned correct answer in my code.
So I don’t quite agree with you. According to the lesson text the code should not be wrong?
But I was able to continue to the next lesson by copying and pasting the solution code into my answer.

You can try my code to understand my confusion.
My code returns an error message if the values are unrealistic - a test for me own sake when typing the code. My output for the spesific values was OK, so I studied the lesson text more carefully.

If (strokes - par) is 3 or more then we leave the loop with ā€œGo Homeā€ as return value. I think it should be OK. The lesson instructions didn’t say we should only test within the array values. The output from my code was OK, but the test said it wasn’t.

Nope. According to the lesson text your code is wrong.

Look at the last line of the chart you were given. You are not using in your code the exact string given in that chart. That sort of thing is easy to overlook, which is why we gave the array of values so you have a way to avoid typos like that.

@Jostein Do you understand what is wrong with the return value or is it still not clear?

Compare the last element in the array with your last return statement. You are missing the punctuation.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.