Golf Code challenge, 2 problems

One, why does golf code expect a ; after my else and before my {?
and 2, why is in not passing with either of the following sets of code?

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 "Hole-in-one!";
}else if(strokes <= par - 2){
  return "Eagle";
}else if(strokes == par - 1){
  return "Birdie";
}else if(strokes == par){
  return "Par";
}else if(strokes == par + 1){
  return "Bogey";
}else if(strokes == par + 2){
  return "Double Bogey";
} else (strokes >= par + 3) {
  "Go Home!";
}

  // Only change code above this line
}

golfScore(4, 1);

which gives:

SyntaxError: unknown: Unexpected token, expected ";" (16:28)

  14 | }else if(strokes == par + 2){
  15 |   return "Double Bogey";
> 16 | } else (strokes >= par + 3) {
     |                             ^
  17 |   "Go Home!";
  18 | }
  19 | 

or, with the ; added where it reccomends:

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 "Hole-in-one!";
}else if(strokes <= par - 2){
  return "Eagle";
}else if(strokes == par - 1){
  return "Birdie";
}else if(strokes == par){
  return "Par";
}else if(strokes == par + 1){
  return "Bogey";
}else if(strokes == par + 2){
  return "Double Bogey";
} else (strokes >= par + 3); {
  "Go Home!";
}

  // Only change code above this line
}

golfScore(4, 1);

which gives

// running tests
golfScore(4, 7) should return "Go Home!"
golfScore(5, 9) should return "Go Home!"
// tests completed

I can copy and paste a nearly identical solution and it works, but I’m not moving on until I know why this doesn’t
The solution from the hints that worked:

if (strokes == 1) {
    return "Hole-in-one!";
  } else if (strokes <= par - 2) {
    return "Eagle";
  } else if (strokes == par - 1) {
    return "Birdie";
  } else if (strokes == par) {
    return "Par";
  } else if (strokes == par + 1) {
    return "Bogey";
  } else if (strokes == par + 2) {
    return "Double Bogey";
  } else {
    return "Go Home!";
  }

what’s the functional difference and why did it tell me to add a ; after my else statement?

1 Like

The syntax issue is actually here.

1 Like

There shouldn’t be a semicolon there. JavaScript is getting confused because an else does not accept a condition in parentheses. It is just

else {
    //do stuff
}
3 Likes

Thanks, I just realized this after scanning this page a few more times. I have to pay more attention lol.

Woops, now that’s a silly mistake.

You’re not the first or the last person to make it. Happy coding!

2 Likes