I copied a bit of the activity I’m referencing for context. I accidentally used assignment instead of equality or strict equality at first.
My code was broken when I had originally put if(strokes = 1)
as the very first condition to the nested statements. I would correctly get the “Hole-in-one!” string when strokes = 1
(which makes sense why that works since I would be assigning 1) but none of the other strings worked.
Why does if(strokes == 1)
process the rest of the nested else/if statements but if(strokes = 1)
escapes the function/not process the rest of the conditions?
Code:
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 {
return names[6];
}
Thanks!