Golf Code alternative to ladder if else

Tell us what’s happening:
is it the only way, that i need to write multiple if else to solve this problem? can anyone simplyfy or optimise the code ?

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(par == 4 && strokes == 1){
    return "Hole-in-one!";
  else if(par == 4 && strokes == 2 || par == 5 && strokes == 2 ){
    return "Eagle";
  else if(par == 4 && strokes == 3){
    return "Birdie";
  else if(par == 4 && strokes == 4){
    return "Par";
  else if(par == 1 && strokes == 1){
    return  "Hole-in-one!";
  else if(par == 1 && strokes == 1){
     return  "Hole-in-one!";
  else if(par == 5 && strokes == 5){
     return "Par";

    return "Change Me";
  // Only change code above this line

// Change these values to test
golfScore(5, 4);

Your browser information:

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

Link to the challenge:

A few things.

  1. Yes, there often are alternatives to a long chain of if/else statements. They may be better or worse depending on the situation.
  2. In your particular case, you have way more if statements than you need. You only need one if statement per possible response. Read the challenge instructions again carefully. For example, if strokes is 1 it’s a Hole-in-one regardless of what par is.
  3. You can use the names array instead of hardcoding the strings to be returned.

Yeah, it’s ugly, but it may be the clearest. Of course, whenever you have a chain of equalities, that is screaming for a switch statement. But there are a few edge cases in there too:

  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";
      return "Go Home!";

Of course, this solution is super DRY:

  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]

That’s a lot DRYer, but is much harder to understand just by looking at it and is therefore harder to debug - I would not recommend it.

1 Like

only single if statement.

One single if statement for each return value, not only one in your code. Look at the description of the challenge again to identify what the condition should be for each return value.

ok… i am done with it. just needed optimisation.

complex and tricky solution. Indeed !

Right now your solution passes the tests, but doesn’t actually work correctly. For example, golfScore(4, 1) should return "Hole-in-one!", but it would return "Change Me".

1 Like

this is not final answer. I have added more code and completed that challenge.
this is just a small part of code ,i wanted to know alternative for this multiple if else.

Okee dokee. I just wanted to make sure that you fully understood this challenge before moving on.

oh thanks mate.
have u completed that certification or on the track?

I haven’t done all the new lessons yet. I also haven’t done the projects because I’m not very motivated to. I’m a professional developer and I’m part of FCC primarily to help teach new aspiring developers like yourself.

great ! I actually was in search of such a senior dev.
Hope you dont mind my further questions as well.

There’s a few of us knocking around on the forum. You’re always welcome to ask questions. You’ll probably also find that many questions that you may have specifically for professional developers have been brought up before. The forum has a really good search functionality.

Yes .I also work for a company but only html-css part I do there because I don’t have much expertise in JS.
So completing fcc certification to get good at JS and TS .like arrow function, ajax, callback ,closure, promise, observables…I want to be master at that. So practicing here.

I now regret posting completed answers. I skimmed the OP question and didn’t examine the code closely enough - I assumed the OP had a completed answer and wanted to know about alternatives. I hope the OP doesn’t copy mine but builds his own.

But yes, a chain of if/else statements is a valid solution, but Ariel is right that your logic was off.

:pensive: may be I didn’t read the post exactly

No, I didn’t mean that as a criticism of you, just that I didn’t read closely enough and I shouldn’t have given you alternative answers until you’d gotten and answer of your own. We’re all learning here.

sure, sir. We should keep learning and help others learn.Thank you !