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);
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
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 ==.
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?
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!
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 : )