Golf Code struggle, pls help

Tell us what’s happening:
I don’t know how to do, pls help me, thanks in advance

Your code so far


var 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!";
  }
  
  return "Change Me";
  // Only change code above this line
}

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

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/golf-code

1 Like

Pretty close!

Make sure you are not using one equal sign as comparison operator. Use three or two.

1 Like

Got it!!! Thanks a lot.

You can fix half of the errors by fixing line 5 and 11.

  • = is to set value to
  • === or == is to compare

Now go to the Bogey, Double Bogey and Go Home! and you can see a issue.

strokes >= par + 1

If i do 4 strokes and 3 par, that is a bogey. My strokes are 1 more then my par.

Now if i do 5 strokes and 3 par, it also returns as a Bogey instead of a double Bogey. This is because your only checking if my strokes is bigger then my par by 1, and not if my strokes is strictly equal to my par + 1.

For example:

function checkNumber(num) {
    if (num <= 10) {
        return "less the or equal to 10"
    }
    if (num <= 7) {
        return "less the or equal to 7"
    }
    if (num <= 3) {
        return "less the or equal to 3"
    }
}

checkNumber(3)

The program will stop at the first if statement and return/ stop the program each time the if statement condition is met. It is not going to skip over it and check the one after it for a less then or equal to 3.

2 Likes

Thanks for your explaination. I passed the challenger.

1 Like

(hit the check in the lower right hand corner of the reply to mark your question as answered)

Thank you! You answer also helped me to pass the challenge (and learn the lesson :wink: )!

1 Like