I don't understand how this code works

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

golfScore(5, 4);

This is code from the site freecodecamp.org, having reached this example, I don’t understand anything about how it all works, for example the first line if hits == 1, but at the same time in the value function 4 I can’t understand it at all, this is javascript

Well, if you hit a golf shot in one shot it means you got a hole-in-one so there’s no need to go any further. In your example you got a birdie Strokes 4 Par is 5 = -1 Birdie

I got to this exercise and there’s nothing clear to me here, <= par - 2 in the function it is indicated (5,4) if this is translated then 4 <= “5-2=>”3 that is, and this again is not clear to me

A bit of a breakdown

const names = ["Hole-in-one!", "Eagle", "Birdie", "Par", "Bogey", "Double Bogey", "Go Home!"];

Arrays are indexed starting at zero.

// names Array
0 : "Hole-in-one!", 
1 : "Eagle", 
2 : "Birdie", 
3 : "Par", 
4 : "Bogey", 
5 : "Double Bogey", 
6 : "Go Home!"

For example

console.log(names[0]); // "Hole-in-one!"
console.log(names[5]); // "Double Bogey"
console.log(names[9]); // undefined (nothing stored in index position 9)

We have a function called golfScore that takes two arguments, par and strokes.The function will return (output) a string e.g. “Hole-in-one!” or “Go Home!”. The string the function returns depends on the values you pass in for par and strokes.

Commented code

function golfScore(par, strokes) {
  // Note: 'return' will output a value and immediately exit the function at that point.
  
  // if strokes equals 1
  if (strokes === 1) {
    return names[0] // exit with "Hole-in-one!"
  }
  
  // if strokes is smaller or equals par minus 2
  // e.g. golfScore(5, 2) a par of 5 minus 2 is 3, and strokes is 2. 
  // 2 is smaller or equal to 3, 2 <= 3 is true
  else if(strokes <= par - 2) {
    return names[1]; // exit with "Eagle"
  }
  
  // if strokes is equal to par minus 1.
  // e.g. golfScore(4, 3) a par of 4 minus 1 is 3, and strokes is 3.
  // 3 equals 3, 3 === 3 is true
  else if(strokes === par - 1) {
    return names[2]; // exit with "Birdie"
  }
  
  // if strokes equals par.
  // e.g. golfScore(2, 2) a par of 2, and strokes is 2.
  // 2 equals 2, 2 === 2 is true
  else if(strokes === par) {
    return names[3]; // exit with "Par"
  }
  
  // if strokes equals par plus 1.
  // e.g. golfScore(2, 3) a par of 2 plus 1 is 3, and strokes is 3.
  // 3 equals 3, 3 === 3 is true
  else if(strokes === par + 1) {
    return names[4]; // exit with "Double Bogey"
  }
  
  else if(strokes === par + 2) {
    return names[5]; // exit with "Bogey"
  }
  
  // if strokes is larger than or equals par plus 3.
  // e.g. golfScore(2, 7) a par of 2 plus 3 is 5, and strokes is 7.
  // 7 is larger than or equal to 5, 7 >= 5 is true
  else if(strokes >= par + 3){
    return names[6]; // exit with "Go Home!"
  }
  
  // if none of the condition above are met then
  // return with "Change Me"
  else {
    return "Change Me";
  }
}

// calling golfScore passing in a par of 5 and strokes of 4
golfScore(5, 4); // strokes === par - 1, outputs "Birdie"

There is a comment at the end of the function // Only change code above this line. This indicates that the code need changing/refactoring. Without knowing what the question is though, it is not possible to give an answer.

1 Like

I understand how to work with an array, but I don’t understand the meaning of these strokes === pairs - 1 or these strokes === pairs or strokes <= pairs - 2

for example, this is what I understand strokes === 1 this means 4 attempts are given if it hits the first time then the name 0 is returned

I have commented your code, please read the comments.

I don’t understand these comments, I don’t understand

Read the code and as you do so, substitute the variables with the values and evaluate the comparisons. You are calling the code as golfScore(5,4) so you will have par=5; strokes=4. Substitute those in the code and you have:

if(4 == 1){
  return names[0]
}
else if(4 <= 5 - 2){
  return names[1]
}
else if(4 == 5-1){
return names[2]
}
else if(4 == 5){
  return names[3]
}
else if(4 == 5 + 1){
  return names[4]
}
else if(4 == 5 + 2){
  return names[5]
}
else if(4 >= 5 + 3){
  return names[6]
}
else{
  return "Change Me";
}

Now evaluate each condition, paying attention to operator precedence.

Is 4 == 1 true? No, so go to the next branch.
Is 4 <= 5 - 2 true? Do the math first, so you have 4 <= 3. It is false, so move to the next branch.
Is 4 == 5 - 1 true? Do the math first, so you have 4 == 4. It is true, so follow this branch.
Return names[2].

1 Like

So… uhm… did you read the previous examples and lessons?

FCC is pretty good at not giving you an example without a basis of understanding it…