Forgive me if i actually asked a dumb question

forgive me if i actually asked a dumb question,but i want to know why the else if was used in returning the strokes and pars instead of only return then with the string??

  **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 "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!";
}

return "Change Me";
// 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/92.0.4515.159 Safari/537.36 Edg/92.0.902.78

Challenge: Golf Code

Link to the challenge:

If else conditional logic helps correspond any given numbers to the return string. It helps create a scenario where the response depends on the input rather than a standard return output.

Good question… In theory, the more efficient bit is either doing it raw or

var names = ["Hole-in-one!", "Eagle", "Birdie", "Par", "Bogey", "Double Bogey", "Go Home!"];
/*Only change code below this line*/

let golfScore = (par, strokes) => (strokes == 1) ? names[0]:  (strokes <= par-2) ? 
names[1] : (strokes == par-1) ? names[2]:  (strokes == par)  ? names[3]:  
(strokes == par+1)  ?  names[4]  : (strokes == par+2)  ?  names[5]  :  
names[names.length-1];  /*callback basic function, mutated to accommodate global par golf score responses...*/

/*Only change code above this line*/
console.log(`Test Case: ${golfScore(5, 4)}`);
 

… I dunno why. Kind of one of those Tony Stark, “Learn to Run Before You Fly” moments… Something for further consideration.

I strongly recommend against a massive one line nested ternary. That is so hard to read and maintain.

2 Likes

Yeah, you write code like that, and you’d better be ready to come out after work to find your tires slashed. :wink:

Addendum: This is in reference to the ternary expression that ate Detroit. Like Jeremey said, that would be a nightmare to maintain. Always try to write your code in a way that someone that has never seen your code can understand it as quickly as possible. And after 6 months of doing something else, that might be you - you might not remember what you were thinking.

1 Like

I’m not sure I understand the OP question

why the else if was used in returning the strokes and pars instead of only return then with the string??

I don’t understand, the strokes and pars aren’t returned.

I will say that this could be cleaner without the elses. Since you are returning (and therefore leaving the function) there is no need to have an else

if (strokes == 1) {
  return "Hole-in-one!";
} 

if (strokes <= par - 2) {
  return "Eagle";
}
// ...

It is a common myth, but you don’t get more effecient code by writing small, obfuscated one-line functions. Efficiency comes from well designed code.

In this solution, I’d use an else because it goes with the style of the if-else statements. There are different ways to express this logic, but none of them are particularly ‘more effecient’ or anything.

Early return with a series of if statements would also work fine, depending upon your preferences. Then I would just leave the last return statement outside of any if or if-else.

Same logic, just a question of what is clear, which partially comes down to the conventions in the code base you’re working in.

2 Likes

thanks your explanation helped

and again why did we have only one of this if (strokes == 1)

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.