Clarify 2 solutions

Can someone clarify/explain the notation/syntax used for solution #2 below? Thx you.
Your code so far


const 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 (strokes <= par -2){
return names[1];
}else if (strokes == par-1 ){
return names[2];
}else if (strokes == par){
return names[3];
}else if (strokes == par+1){
return names[4];
}else if (strokes == par+2){
return names[5];
}else{
return names[6];
// Only change code above this line
}
}

golfScore(5, 4);

Second solution-

const names = ["Hole-in-one!", "Eagle", "Birdie", "Par", "Bogey", "Double Bogey", "Go Home!"];

function golfScore(par, strokes) {
  return 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[6];
}
  **Your browser information:**

Challenge: Golf Code

Link to the challenge:

The second solution is using the conditional ternary operator and to be honest, you would never use this solution in the “real world” because it is too hard to read.

1 Like

perfect, thx you!!! that helps.

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