Golf Code. Could someone explain me the logic of this code?

Golf Code. Could someone explain me the logic of this code?
0

#1

Could someone explain me the logic of the code?


var names = ["Hole-in-one!", "Eagle", "Birdie", "Par", "Bogey", "Double Bogey", "Go Home!"];
function golfScore(par, strokes) {
 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!";
  }
}

// Change these values to test

alert(golfScore(4,5));

I know it’s correct but could someone explain me why does “(4,5)” returns “Bogey”(as an example)? And why each number returns the expected value. I don’t seem to understand the logic. Could someone clear my mind?
I don’t seem to understand the logic like:
golfScore(4, 1)should return "Hole-in-one! golfScore(4, 2) should return “Eagle”
golfScore(5, 2)should return "Eagle" golfScore(4, 3) should return “Birdie”
golfScore(4, 4)` should return “Par”

I took this code from here https://guide.freecodecamp.org/certifications/javascript-algorithms-and-data-structures/basic-javascript/golf-code/


#2

well let’s start from the beginning
par = 4 and strokes= 5

if (strokes == 1)

strokes equals to 5, which means the statement above is not true

else if (strokes <= par - 2)

5<=4-2
5<=2 not true

else if (strokes == par -1)

5 == 4-1
5 == 3 not true

else if (strokes == par)

5==4 not true

else if (strokes == par +1)

5==4+1
5=5 true

then

{return "Bogey";}

I don’t know if it makes sense for you or not. If not just ask more quetions and i will try to respond.


#3

I get it now. Thanks!