Basic JavaScript - Golf Code

Tell us what’s happening:

Describe your issue in detail here.

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 (par===4&&strokes===1){
  return "Hole-in-one!";
}else if((par===4||5)&&strokes===2){
  return "Eagle";
}else if(par===4&&strokes===3){
  return "Birdie";
}else if(par===1&&strokes===1){
  return "Hole-in-one!"
}else if(par===4&&strokes===4){
  return "Par";
}else if(par===4||5&&strokes===5){
  return "Par";
}else if(par===4&&strokes===5){
  return "Bogey";
}else if(par===4&&strokes===6){
  return "Double Bogey";
}else if(par===4&&strokes===7++){
  return "Go Home";
}else{
  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/119.0.0.0 Safari/537.36

Challenge Information:

Basic JavaScript - Golf Code

2 Likes

Remember, the par passed into the function can be different numbers, it won’t always be 4. Short holes are usually par 3 and longer holes are often par 5. So I’m not sure you really want to limit this to only holes that are par 4.

This isn’t how you say “if par equals 4 or par equals 5”. You literally have to spell it out the way I said it between the quotes.

This only works if the par is 4 and the strokes is 5. What if the par is 3 and the strokes is 4?

What you are doing here is hard coding the possible numeric combinations into the code. Instead, think about how you can compare the par to the strokes passed into the function without having to know what their actual values are.

3 Likes

I changed it so its not limited to specific values but so that it’s more abstract like in the video but still doesnt work.

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

golfScore(5, 4);

Hello @andres.e.suarez.82 !

This is a tricky one, but you are very close to solving the challenge.

Your boolean operators need some consideration. Think about how a value < par can return different values.

Your code says

if the value of strokes is <= -1 then return “Birdie”, but -2 is also less than -1.

A similar situation also exists for strokes greater than 1.

Does this help?
Keep up the good progress!

Happy Coding! :slightly_smiling_face:

2 Likes

Best I could do was remove smaller and greater than and equal -1 and +1 but still (4,7)&&(5,9) not working.

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

golfScore(5, 4);

Wait, there was some “== +2” missing, I found it. Thanks a lot. It was impossible. Thank you.

1 Like

You’re welcome.
So, it’s working now?

1 Like

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