Basic JavaScript - Golf Code

when i press run the test button it doesn’t do anything
Can you please help me.

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 ["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 (strokes>=par+3) {return ["Go Home!"] };




  // 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/118.0.0.0 Safari/537.36

Challenge: Basic JavaScript - Golf Code

Link to the challenge:

Please Tell us what’s happening in your own words.

Learning to describe problems is hard, but it is an important part of learning how to code.

Also, the more you say, the more we can help!

Conventional formatting:

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

golfScore(5, 4);

i copied paste your code and still doesn’t want to run the test

That’s because I didn’t write the answer for you - that’s against the rules. I just fixed your formatting.

Can you try to talk about your code, the errors you see, and what you have tried to fix them?

in the beginning i forgot to add , so i add those to the name, you helped me with the format and now…i look at it and i don’t see it…i don’t see what’s wrong

Why are you adding []?

so i can get the answer in in quotation marks

You don’t need [] to use ""

i mean yes…to give a value to the return…return “Eagle”

A return value doesn’t have to include []s

3 Likes

The primary problem was in the last else statement. You should not specify a condition after else. Instead, you should directly use else to catch all cases where none of the previous conditions match. Additionally, the corrected code includes a console.log statement to see the result of the golfScore function. I have used this tool codebeautify org’s javascript-tester.

3 Likes

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