Golf is too WET?

I was working on the golf JS challenge and it seems like it could be dried up but I’m not sure how? Any pointers? Here’s a snippet:

 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!";
  } else {
    return "Change Me";
  }

How would you recommend drying up this code or am I just being neurotic?

I’ve edited your post for readability. When you enter a code block into the forum, precede it with a line of three backticks and follow it with a line of three backticks to make easier to read. See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.

markdown_Forums

1 Like

What is coming up as wrong (bottom left below enter)?

Is it a function and strokes & par are parameters? You cannot just have a couple if statements without calling them.

You are mighty close. Like really close. Like all the If statements are correct.

function golfcode(strokes, par) {
return strokes+par // 20
// you can also do other things with strokes and par (hint you if statements)
}

golfcode(15, 5) // 20

See what i mean by parameters and function?

1 Like

Yeah, whenever I see a chain of if/else, my first questions is if I can replace it with a switch statement:

  if (strokes===1)
    return "Hole-in-one!";
  if (strokes<=par-2)
    return "Eagle!";
  switch (strokes-par) {
    case -1:
      return "Birdie";
    case 0:
      return "Par";
    case 1:
      return "Bogey";
    case 2:
      return "Double Bogey";
    default:
      return "Go Home!";
  }

Is it better? I dunno. But it is inherently messy logic so of course the code is going to get messy. You have one condition based on the absolute strokes, the rest are based on the strokes relative to the par, and only 4 of those are absolute equality, plus a greater than and a less than. Of course this is going to get messy. Yeah, we could hide some of that messiness with some added complexity, but that would be readable. This isn’t pretty, it isn’t elegant, but it is clear.

Sure, I could write something like:

  const msgs = ["Hole-in-one!","Eagle!", "Birdie", "Par", "Bogey", "Double Bogey", "Go Home!"]
  let score=strokes-par
  return strokes===1?msgs[0]:score<=-2?msgs[1]:score>=3?msgs[6]:msgs[score+3]

It is dryier. Is it better? Not at all. I would hate to come across a bunch of code like that. DRY and compact and taciturn are all good goals, but they have to be balanced.

1 Like

Thanks for the time it took to respond! Nothing was actually broken, and I left out the function name here, sorry. I can’t remember the name but say it was called golfScore and the parameters were par and score. I was basically seeing if there was a drier way to write it. @kevinSmith great point! Thank you!