Tell us what’s happening:
Can’t figure why this isn’t running?
EDIT: I have realised by looking at another solution, that on my final ‘else’ condition, removing the (strokes >= par +3) argument and just having 'else {return names[6];} seems to work. Why is this?
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 (strokes >= par +3) {return names[6];}
// Only change code above this line
}
golfScore(5, 4);
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.
But why is it an issue? I see that without the condition the code passes. I can’t see why though, practically say is par, strokes were (5,10) then both statements with or without the condition should return the statement “Go Home!”? No?
An unconditional else can only be used at the end if there’s no condition; if there’s a condition, you’ll still need an else if regardless if it’s at the end or not.
And yup, it’s not necessary to have an else at the end of an if.
With regards to the condition on why it is not necessary to have an if at the end, it’s because your if/else have conditions for all possible values except those that are above par + 3 . Thus, at the end of the if/else block, even without the else if, you can be certain that the only possible stroke value would be above par+3 and it will return “Go Home!”.
The final “else” statement is made without a condition. It simply means…run “this code” after you have exhausted all my other conditions I stated above. You don’t use it with a condition.