Build a Golf Score Translator (full-stack curriculum) - Strokes = Par failing

I have no idea what the problem is. My tests pass up until this one, where strokes equal par.

The step is:

golfScore should return "Par" if strokes is equal to par.

My answer for this one is

else if (str === par) {
return names[3]; // where "Par" is the fourth element of the names array
}

The test results are

Failed: 13. golfScore(3, 3) should return the string Par
Failed: 14. golfScore(4, 4) should return the string Par
Failed: 15. golfScore(5, 5) should return the string Par

This answer uses the same logic and syntax as my previous ones, which all passed.

Can someone tell me what I might be doing wrong?

Mod edit: Build a Golf Score Translator: Build a Golf Score Translator | freeCodeCamp.org

2 Likes

Hey @ryanqpublic,

Can you post all of the code that you currently have for this project?

Sure. The first line - the creation of the array - is given to us. The rest is my answer up to and including the line that fails the test.

const names = [“Hole-in-one!”, “Eagle”, “Birdie”, “Par”, “Bogey”, “Double Bogey”, “Go Home!”];

function golfScore(par, str) {
  if (str === 1) {
    return names[0];
  } else if (str <= (par - 2)) {
    return names[1];
  } else if (str = par - 1) {
    return names[2];
  } else if (str === par) {
    return names[3];
  }
}

Another quirk I noticed is that, if I check my answer up to this point, all the tests up until the ones for “Par” pass. However, if I post all of that plus the rest of my answer, ALL tests fail.

I already know the first several lines are correct because they pass when I post only them. So there’s something wrong with the tests themselves where, if enough later answers are incorrect, all will fail the test, including the earlier ones that are correct. It makes it much harder to determine which parts of my code are incorrect.

I also noticed this problem with the testing in an earlier assignment.

Hi @ryanqpublic ,

Can you say what this condition is doing?

Happy coding!

That line is my answer to the instruction,

“golfScore should return "Birdie" if strokes is equal to par minus 1.”

But that’s not what your code is saying. Look closely.

Log it to see.

Oooh, I see what you mean. That speaks to the other problem I mentioned above. The tests themselves are not accurate. When I include my answer up to and including the one for “Par,” “Par” fails the test and “Birdie” - using that code you just pointed out - passes. What should be happening is “Par” passes and “Birdie” fails. So I can’t use the tests to help me find my error. Luckily, you did. Thank you!

I’m spelling it out here because it seems the tests are broken and provide no logical way for someone else to figure it out without being told: that line dhess pointed out should read

(str === par - 1)

The tests are simple tests and cannot account for every variation they encounter. Do not try to code based only on the tests. If a test is failing, you need to check that the User Stories are implemented correctly.

Nor should you.

You should be testing your code by logging function calls with different scenarios at the very least. Also, log variables and conditions within a broken function to see if they are what is expected.

There will be no tests to rely on when you’re on the job (unless you write them). It’s up to you to debug your own code.

Happy coding!

As someone whose entire knowledge base is the lessons up until this point, I don’t really understand what you’re suggesting a person should do in this situation (i.e. logging function calls in different scenarios). My grasp of the terminology is limited.

There are tests for each line of instruction. If a line passes a test and the following one fails, I would think I should be able to assume it’s because the first line is correct and the second one is incorrect. That seems like the most logical way to determine where an answer went wrong.

console.log(golfScore(4, 1)) // Hole-in-one!
console.log(golfScore(4, 2)) // Eagle

use console.log, a lot

to start tho you can use a tool like https://pythontutor.com/ to help you visualize what your code does line after line

I think we all use the pass or fail of individual tests to try to figure out what we did wrong and how to fix it. I don’t think it should be the only tool, but it is a tool that is usually useful in this situation. Maybe there should be a stronger emphasis on using console.log liberally in labs when not prompted to, I kinda thought it would break the tests to use in a lab or workshop so I never tried if the instructions didn’t say to.

sometimes the tests can fail if there are extra console.logs, so you can use console.log for debugging then remove them before running the tests if the tests are checking console.log output (most often they are not)

1 Like