Why is a strict equality operator not plausible for the golf challenge?

Continuing the discussion from freeCodeCamp Challenge Guide: Golf Code:

Regarding the problem solving for lesson 182:

Here is my answer:

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

I used the strict equality operator. Why is that not appropriate for this lesson?

Here is the answer to the problem:

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

Also, I don’t understand why an eagle is less than or equal to (<=) par -2.

1 Like

I did finally get the answer but I was still wondering about why the strict equality operator is not appropriate here and why an eagle is classified as I had stated before.

There is no problem with strict equality. If you take the answer that you have listed in your post and change all the == to === it still passes. Really strict equality is preferable. The only problem with your solution was that you were returning the variable par when you should have been returning the string "Par".

As for the definition of “eagle,” maybe it’s not exactly the right definition of the golf term (I have no idea. I don’t golf). Someone pointed out recently that there’s actually another funky name that isn’t included in the challenge. You can create an issue for it on GitHub if it bugs you, but the point is practicing logical operators so the whole golf thing is just a convenient way of setting up the challenge.

1 Like

Guys on such exercise what seems to be logical may be a bit confusing for someone new to programming. So not to give spoilers of the code. The logic is.

You are trying to check what stroke is relative to what par. In other words what you are trying to achieve is:

if as stroke is equal (==) to par. Tel the user it is a "Hole in one!"


Which translate to code as:
if(stroke == par) {return "Hole-in-one!";}
Always comparing which stroke is relative to which par (and the math behind each par).
>I hope it helps ;)
3 Likes