Golf Code - Code not compiling

Golf Code - Code not compiling
0

#1

Tell us what’s happening:
Hi,

Can anyone help me with why this code isn’t compiling? I’m struggling to work out why…

Thanks. Nikki

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
  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];
}
  // Only change code above this line
}

// Change these values to test
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/67.0.3396.87 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/golf-code


Golf Code not compiling
#2

It is working fine for me try reloading the page and resetting the code…


#3

Oh ok… at least the code isn’t wrong… what browser are you using? I’m in Chrome… thought it was because I’m running an incognito window, but moving to a normal one won’t work for me either :thinking:


#4

Is there anyone else who could check over this? I’ve asked support to look at it and they’re saying if I’m using chrome (which I am) it’s likely to be an issue with the code rather than a support issue.

Thanks :slight_smile:


#5

Hi,

I’ve been trying to get the Golf Code lesson to compile. I’m using Chrome. Someone has replied to my help request to say they’ve pasted it in and it works fine for them, but i can’t get it to progress…

Can you help me to come up with some options to move forward please?

Thanks, Nikki


#6

Are you by any chance using Edge, IE, or Safari as your browser? These have decided not to implement new features of JS and CSS according to web standards and as a result they often exhibit unintended (and frustrating) behavior.

Just a little vocabulary thing: the problem isn’t that your code is not compiling. JavaScript isn’t a compiled language. Either the tests are not running (this would be a browser issue) or the tests are not passing (this would be an issue with your solution).


#7

Thanks Ariel. Tip on the language is welcome :blush:

I’m not… I’m using Chrome… It’s been fine for all the other tasks. I’ve tried resetting the code 3 or 4 times… logging out and back in hasn’t helped either :thinking:


#8

If you’re using the latest version of chrome and you don’t have any settings or extensions that block scripts from running, then the tests are probably running and failing. I know that you said that someone else ran your code, but if there’s nothing funky about your browser then it’s something about the code itself.


#9

ok… I’ll try again. Maybe someone else can run it to test too… I can’t see any mistakes, but I’m no expert. I’ve pasted it below, although I’m not expecting help, just for your reference. I’ll post in the JS forum again and see if I can get help there… Thanks Ariel.

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

// Change these values to test
golfScore(5, 4);

#10

Here is your error:

  } else (strokes >= par+3) {

An else statement doesn’t take a condition. An if statement does. (An else if is really just an else followed by an if.)


#11

Thank you, Ariel! That’s really helped…


#12

Glad to help. Happy coding!