Need explanation with the output of a function

Need explanation with the output of a function
0

#1

I don’t understand the output of this code:
https://pastebin.com/9MsiSiQs

For example I want the run the the if statement, what should i put in the output of the function?

Can someone please explain to me.


#2

Output of a function is whatever comes after return keyword. If you have it inside if statement, it will stop execution of the function. Basically here you have “if this - output that” vs classic “if this - do that”. Hope this makes sense.


#3

If you are trying to get the first if statement to execute:

if (strokes == 1){
    return names[0];
  }

then

golfScore(par, strokes);

on line #27 in your code needs to be

golfScore(anyNumberYouWant, 1);

because what par is does not matter, only the fact that strokes is 1. You are passing par and strokes into the function golfScore on line #2 and the values you assign to them in line #27 is what the function uses for those words anywhere you see them in lines 4-22


#4

Thank you! I was struggling on this.


#5

You’re welcome. Next time, you should actually put the code into the question, instead of putting a link to it on an external website. Just type three backticks, then type or paste code, and then type three more bacticks after your code so that we ensure everyone is always able to reference and learn from what your question was.


#6

Mr_Li’s question was in regards to the following code:

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];}
  // Only change code above this line
}
 
// Change these values to test
golfScore(5, 4);

#7

Sure, i will do next time.
Thanks.