JS golfcode error below 2nd array

Tell us what’s happening:

to return the value “Par” both the stroke and par should be the same eg:(4,4)or (5,5) i used the equality operator but the the console says it’s wrong

  **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 names[0];
  } else if (strokes <= par - 2){
    return names[1];
  } else if (strokes = par - 1){
    return names[2];
  } else if (strokes === par ){
     return names[3];
  } else if (strokes == par + 1){
    return names[4];
  } else if (strokes == par + 2){
    return names[5];
  } else {
      return names[6];
  }
  return "Change Me";
  // Only change code above this line
}

golfScore(4,4);



      **Your browser information:**

User Agent is: <code>Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.107 Safari/537.36</code>

**Challenge:**  Golf Code

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

Hello there.

Do you have a question?

If so, please edit your post to include it in the Tell us what’s happening section.

Learning to describe problems is an important part of learning how to code.

Also, the more information you give us, the more likely we are to be able to help.


= is assignment. === is comparison

Also, I don’t think this does what you think it does.

yes it is not supposed to be there sorry i made a mistake and i forgot to change that it should be the same as else if (strokes = par - 1) instead with a + operator

But why use = instead of ===?

The code

a = b

sets the variable a to be equal to the value of variable b.

The code

a === b

checks to see if a is equal to b.

i mistook it for equality operator

i used the strict equality operator the error remains the same

What is your updated code? You had a few errors.

omg i made a huge mistake i just reread your comment about equality operator and read the code back from top to bottom that’s when i realised i used assignment operator in the second else if statement strokes = par - 1

i feel so stupid i was looking below 3rd else if statement because the value “Birdie” was displaying without a problem.

thank you sir so much i fixed it

We’ve all been there. Good job catching it!

once again thank you for the help

1 Like

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