Evaluation in the golfer problem

Tell us what’s happening:
I am curious why my code doesn’t work? It seems it is doing the same as the solution, except the math evaluation is switched.

  **Your code so far**

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 "Eagle";
}else if (strokes - par == -1){
return "Birdie";
}else if (strokes - par == 0){
return "Par";
}else if (strokes - par ==  1){
return "Bogey";
}else if (par = strokes - par == 2){
return "Double Bogey";
}else{
return "Go Home!"
}

return "Change Me";
// Only change code above this line
}

golfScore(5, 4);
  **Your browser information:**

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

Challenge: Golf Code

Link to the challenge:

Oops. What does this do?

Ah, so the first eval, throws it off? Let me try fix that.

1 Like

Ah, duh. I did assignment “=” not evaluation “==”. Changing this worked!

There you go! Nice work getting it passing!

Side note - generally you should prefer === over ==. Sometimes == can have some mildly surprising results due to type coercion.

Excellent, thank you! Will do!

1 Like

this is also weird - did you meant to assign a new value to par?

1 Like

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