Hey all, just a little confused as to why i can’t use = instead == in this function.
Also - why is “Eagle” strokes <= par - 2? Shouldn’t it just be == par -2? if it was less than -2 - say par - 3 it would be a Hole-in-one?
Thanks!
Matt
Your code so far
var 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 if (strokes >= par + 3){return names [6]}
// Only change code above this line
}
console.log(golfScore(5, 9));
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.129 Safari/537.36.
Single = : assign the value on the right to the variable on the left
Double == : loose comparison of the two sides, do they evaluate to the same result
Triple === : strict comparison of the two sides, are they the same thing and same type
In this case, you can’t use = for comparison. You’d always get a true on the first if since myVar = myVal is truthy so long as the assignment is successful.
Since others have already answered the = vs == vs === question, I’ll answer your second question:
Your question assumes that the highest par value is 4. If that were true, then yes, par-3 would be a hole in one. If it’s a hole-in-one, the function terminates with the first condition of the if statement , so the second and subsequent lines will not run.