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

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

#1

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.


#2

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.


#3

Of course you can use strict equality operator to solve this challenge. That is not the reason you were not passing the all tests. The reason you were not passing all the tests, is the code on the following line:

  else if (strokes === par) {return par;}

You are not returning the string “Par”.

I do not know why an eagle is considered <= par -2. All I know, is that is the way the challenge is defined, so you have to work within the constraints of the specified problem.


#4

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.


#5

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