Please help with "Golf Code"

Hi, Any help will be appreciated.
Trying my best to not use hints lol, I’ve searched through previous questions on this challenge but not finding any for the last four parts of the challenge.

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

function golfScore(par, strokes) {
  // Only change code below this line
par = 4;
if (strokes <= 1) {
  return "Hole-in-one!";
}
else if (strokes <= 2) {
  return "Eagle";
}
else if (strokes < 4) {
  return "Birdie";
}
else if (strokes = par) {
  return "Par";
}
else if (strokes == par + 1) {
  return "Bogey";
}
else if (strokes == par +2) {
  return "Double Bogey";
}

else if (strokes >= par + 3) {
  return "Go Home";
  }


  // Only change code above this line
}

golfScore(5, 4);

link to the challenge

1 Like

Tip for you and others reading this: When you have these kind of programs, where you return different values, depending on variable you are checking, which can have lower or higher vallue, ALWAYS start from higher values to lower, otherwise it won’t work! :smiley:

Why? Why overwrite the value of par?


Woops.


Why this 4?


Why 2?


Higher to lower wouldn’t work here since we are looking for values under a target initially.

Try this, it was working for me. And don’t overwrite values of parameters.

@JeremyLT
Yes, it will work If he starts from higher to lower, but he also has bug when he has overwritten value of par.

No. Hole in one (smaller) must come before eagle (bigger). The rest of the cases can come in any order.

1 Like

noted… thanks. I feel dumb with learning javascript lol

1 Like

now that you ask, I would say I don’t know, i Think in my mind I had to somehow assign a value to par lol.

Honestly, this was probably my 4th attempt on the code, I had different code there on previous attempts by this time I was really just trying different things to see what works to get the others to pass.

that was what i thought i understand from these instructions:
" Return the correct string according to this table which lists the strokes in order of priority; top (highest) to bottom (lowest):"

The value of par is set when you call the function. golfScore(4, 5) sets par = 4 and strokes = 5, but you don’t need to set those values inside of your function.

You nearly never want to use a = in a if comparison. You want a ===.

For ‘higher’, it depends on what you mean by ‘higher’. If you are talking about ‘higher’ with respect to numbers, then usually ‘higher’ means ‘bigger’. In this case though, the problem is talking about ‘higher priority values in the chart’ and you were already listing the cases in that order.


No need to get down on yourself - this stuff is hard and learning takes time!

1 Like

LOL that’s like that when you learn new language. I was feeling like that while learning C, and it’s harder than JS! :joy: :joy: :joy: :joy:

1 Like

I understand now, Thank you for the explanation, this was covered in previous lessons but I promise you, when I get to a challenge such as this one, It’s like I’ve learnt nothing, I get completely lost.

I think I learn better with doing little projects along with a tutorial as I did with HTML and CSS but the problem is that I at least understood most of what was being taught, now I’m just clueless. lol

Haha please don’t say that… I really thought and was hoping that JavaScript is the hardest lol.

1 Like

When you think JavaScript is hard, always have on mind: there is harder one. :smiley:

1 Like

In some ways I find C easier than Javascript. It is a much simpler language with fewer pieces of syntax. But because it is simpler, you have to mange more stuff yourself rather than using built in functions.

1 Like

:sweat_smile: :sweat_smile:

1 Like

Just want to thank you again, I went back to redo the challenge after I gained the new understanding from you and just had a bit of difficulty with the “Go Home!” section but that didn’t take long to get right.

also used ‘==’ only instead of ‘===’ on one or 2 of the ‘else if’ comparisons but it still passed.

I see the value of asking for help in the forum now lol.

Hope you guys won’t be hearing from me too soon again though. lol

You shouldn’t use ==. It can lead to some surprising results. === is better.

1 Like