Problem - Golf Code -Please advise

ello,

Thank you in advance for your time.

I’m not able to see why I am getting an error.
I do not understand why I am getting the error shown at the end of this message.

  **Your code so far**

var names = ["Hole-in-one!", "Eagle", "Birdie", "Par", "Bogey", "Double Bogey", "Go Home!"];
function golfScore(par, strokes) {
// Only change code below this line
    return names;
  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];
  }
  }
golfScore(5, 4);
  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:92.0) Gecko/20100101 Firefox/92.0

Challenge: Golf Code

Link to the challenge:

ERROR CODE -

// running tests

golfScore(4, 1)

should return the string

Hole-in-one!
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(1, 1)

should return the string

Hole-in-one!
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

Hi @eusipease !

Welcome to the forum!

The issue is here

You have a return statement before the if/else.
A return statement ends the function.
So the if/else is not happening.

You need to delete that

Hello and thank you! That fixed my problem, for the most part. My final solution is below and it passed.

I have a question about the two of the “else if” statements, italicized in the output below. Why did I have to add the “&&” to explicitly check to see if the stroke was equal to 3 or 4 for the function to work? It seems like that will limit the program for other possible inputs. Or am I missing a huge point of this lesson like that I should only care about the test scenarios?

Thanks.

Here is my final code:

var names = ["Hole-in-one!", "Eagle", "Birdie", "Par", "Bogey", "Double Bogey", "Go Home!"];
function golfScore(par, strokes) {
  // Only change code below this line
     // return names;
    if (strokes === 1) {
      return names[0];
    } else if (strokes == 2) {
      return names[1];

    } else if (strokes == par -1 && strokes == 3) {
      return names[2];
    }else if (strokes == par -1 && strokes == 4) {
      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];
    }
    }
golfScore(5, 4);

that’s not correct, you are hardcoding stuff. The first version even with the return statement, was more correct.

I agree and that is my question. When I submit the original it tells me that the following tests do not pass:

golfScore(4, 3) should return the string Birdie

golfScore(4, 4) should return the string Par

My question is why does it not pass?

Because you are incorrectly hardcoding your function against the answers.

Let’s back up and throw away the hard coding approach.

Your original code was closer:

1 Like

Ok, I started with a fresh window and input my original code minus the line I needed to delete. It worked.

I wish I had a copy of what was not working because I really want to know what the issue was.

Thanks again for everyone’s time on this. Here is the final input that passed:

var 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];

  }

  return "Change Me";

  // Only change code above this line

}

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

Thank you!!! I will be more careful moving forward :slight_smile:

1 Like

A post was split to a new topic: Help with Golf Code