Basic JavaScript: Golf Code (*help)

Hey guys, lately I’ve been having issues with the site not reading my code one bit. I’m pretty sure there’s nothing wrong with my code its just not reading it for some reason.

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 (par <= - 2) {
  return names[1]
}
 else if (par - 1) {
  return names[2]
} 
else if (par) {
  return names[3]
}
 else if (par + 1) {
  return names[4]
}
 else if (par + 2) {
  return names[5]
}
 else if (>= par + 3) {
  return names[6]
}

// Only change code above this line



golfScore(5, 4);

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.75 Safari/537.36.

Challenge: Golf Code

Link to the challenge:

definitely, this line is not correct else if (>= par + 3) { return names[6] }
the correct format of this line should like this (strokes >= par+ 3)

These conditions don’t do what you think they do. You should compare to the number of strokes.

This is not a valid logical condition. You need something on the left hand side.

Thanks guys, here is an update (still not working)

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 if (strokes >= par + 3) {
    return "Go Home!";
  }
  
  // Only change code above this line



golfScore(5, 4);

Are you missing a closing } for the function?


Side note, the traditional syntax is

if (test1) {
 //...
} else if (test2) {
 //...
} else {
 //...
}

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

The closing } was my issue! Thanks a ton!!
Also thanks haha I’m still new to the way these forums work :rofl:

1 Like