Basic Java Script Golf

I have one question and one query.

First why can I just not use the = operator in this challenge ; I thought === was only needed when strings and number were involved and strict comparison was necessary.


I coded this in a way I find more logical. Whilst the code appears OK according to the
console, when it is run it says every combination is wrong but I dispute that it is wrong.

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 === 0){
    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);

What am I missing here please ?

Its really hard to say much without seeing your code. Thanks for adding your code.

= and === are different. = is for asignment. == is legacy and shouldn’t generally be used. === is for comparison.

What does this mean?

This is not a comparison

Thank you. I will use === ( = comes from my background coding years ago )

I might had solved this myself and earlier but for being mis led by the console output:-

// running tests
golfScore(4, 2) should return the string Eagle
golfScore(5, 2) should return the string Eagle
golfScore(4, 3) should return the string Birdie
golfScore(4, 4) should return the string Par
golfScore(5, 5) should return the string Par
golfScore(4, 5) should return the string Bogey
golfScore(4, 6) should return the string Double Bogey
golfScore(4, 7) should return the string Go Home!
golfScore(5, 9) should return the string Go Home!
// tests completed

As you can see the use of the equal sign in the IF logic was not faulted but very other Else logic was. The reverse was true. Once I changed the IF to === the rest of the code worked just fine. This I find misleading.

Nonetheless many thanks for your answer and the promptness of it too. I only just got back to working just now,


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