# Basic JavaScript - Golf Code (Solved it but didn't clicked)

So I solved the challenge but have a couple of questions… So how come if strokes argument is 4 it is equal to 1? Doesn’t make sense to me. Shouldn’t it be not equal because strokes if number 4 and the other is number 1?

Likewise with the second if statement, if strokes is 4 then how is it less than or equal to 3?

Same thing with the fourth, fifth and sixth if statement, strokes is assigned 4 how is it strictly equal par 5? stokes is again 4 par is 6. strokes is 4 par is 7.

Any help would be greatly appreciated. I’ve been scratching my head for a hot minute.

``````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 {
return names[6];
}
// Only change code above this line
}

golfScore(5, 4);
``````

User Agent is: `Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36`

Challenge: Basic JavaScript - Golf Code

Strokes won’t always be 4. Every hole will have different par and strokes values.

If you take 3 strokes to sink the ball and the hole is par 2, you will call `golfScore(2, 3);` Strokes is 3 and par is 2. So, strokes == par+1. `if (strokes === par+1)` this code will execute.

If you take 5 strokes to sink the ball and the hole is par 3, you will call `golfScore(3, 5);` Strokes is 5 and par is 3. So, strokes == par+2. `if (strokes === par+2)` this code will execute.

Ok but how do we determine which value each par and stroke will have for each individual statement?

Here you are calling the function and passing it two values, 5 and 4.

``````function golfScore(par, strokes) {
``````

Here the function is defined to receive those two values and assign them to the variables `par` and `strokes`. Now `par` = 5 and `strokes` = 4.

``````else if (strokes === par-1) {
return names[2];
``````

When the program gets to this line it evaluates it as:

``````else if (4 === 5-1) {
return names[2];
``````

5-1 = 4 is true. So it will execute that line and return `names[2]` which evaluates to `"Birdie"`

Yes I already got that but I was talking about the other statements actually

Do you mean why does `par-2` `return names[1] ` “Eagle”?

Can you rephrase the question, I guess I didn’t understand it.

So bacically what I’m wondering is how come strokes(4) is strictly equal to par(5)? In these statements below :

else if (strokes === par) {
return names[3];
}
else if (strokes === par+1){
return names[4];
}
else if (strokes === par+2){
return names[5];
}

It should return false not true because it is not strictly equal. Hope I made myself clear.

This structure will skip over lines where the expression in the brackets is false.

`golfScore(5, 4);` par = 5 and stokes = 4

``````if (strokes == 1)
``````

(4 === 1) is False so this gets skipped and it moves to the next line:

``````else if (strokes <= par-2) {
``````

(4 === 5-2) is False so this gets skipped and it moves to the next line:

``````else if (strokes === par-1) {
``````

(4 === 5-1) is True, so only in that case it will execute the part in the curly brackets:

``````{
return names[4];
}
``````

Then it breaks out of the If/Else structure and continues to the end of the program. Only the If statement where the expression in the () brackets is True gets executed and breaks out, everything else is skipped over. If it skips everything and gets all the way down to the Else statement, it will execute that as a final catch-all.

2 Likes

I see… So it only returns one argument [birdie] because it is true. Now it’s crystal . Thanks stranger.

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.