Golf code. Cant find the mistake

So, i did my Golf code like displayed below, and i can’t understand why is it wrong. I was wondering if it was something with the vars ‘x’ and ‘y’, but since the “Hole in one!” works perfectly, i cant figure out whats the problem with the other situations.
Appreciate any help, and really sorry if its something stupid.


var names = ["Hole-in-one!", "Eagle", "Birdie", "Par", "Bogey", "Double Bogey", "Go Home!"];
function golfScore(par, strokes) {
// Only change code below this line
var x = par;
var y = strokes;
if (y=1){
  return names[0];
} else if (y<=(x-2)){
  return names[1];
} else if (y=(x-1)){
  return names[2];
} else if (y=x){
  return names[3]
} else if (y=(x+1)){
  return names[4];
} else if (y=(x+2)){
  return names[5];
} else if (y>=(x+3)){
  return names[6];
} else{
  return "Change Me";
}
// Only change code above this line
}

golfScore(5, 4);

Challenge: Golf Code

Link to the challenge:

So the problem here is that you are using single = instead of using triple ===.
Single = is only for assigning a variable
Double == is comparing for loose comparison
Triple === is for strict comparison
So use === instead of single =
It is fine to only use single if you want to compare if one is bigger than the other like <= or >=
If you want more info, me and other people have explained it here

Why can't i use the = instead of == operator in this function? - #2 by JeremyLT

1 Like

You’ve got two things going on here

  1. You have instances where you are using y=(....). Single equals is assignment (i.e. make y contain the value of your expression). You need to use comparison ==.
  2. Your final else if really should just be an else.

A question - why did you change to y and x when strokes and par are much more descriptive and readable?

2 Likes

Oh, sure!! 100% inattention! Really appreciate the help @Catalactics!

1 Like

Thank you very much @JeremyLT!! It really was a dumb mistake. For your question, tbh i just wanted to make it a bit quicker by typing less characters in each “if”. But i definitely get your point! Really appreciate your help man!

2 Likes

By quicker do you mean quicker to type? The computer doesn’t really care how long your variable name are : ) (well, unless they are so long they don’t fit on your harddrive :D)

Also, we’ve all mixed up = and ==. Now you’ll remember it better next time : )

2 Likes

Yes, just to type. It was a lazy move that i will stop doing right now, so i dont get bad habits.

Thank you man! You’re right, i wont make the same mistake next time! Appreciate. Peace :slight_smile:

1 Like