Golf Code JS Challenge

Hi

I am having trouble with my following code. Some of the tests are passing but others aren’t.
Thanks

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

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

// running tests

golfScore(4, 4) should return “Par”

golfScore(5, 5) should return “Par”

golfScore(4, 5) should return “Bogey”

golfScore(4, 6) should return “Double Bogey”

golfScore(4, 7) should return “Go Home!”

golfScore(5, 9) should return “Go Home!”

// tests completed

You’re using assignment in some conditionals.

Remember assignment is one =, comparison is ===.

Still not able to get it to pass. My Eagle and Birdie are passing but nothing else

Please post your updated code. Make sure to wrap them between ``` (three backticks) to format.

Here is my code & error message:

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

// running tests

golfScore(4, 4) should return “Par”
golfScore(5, 5) should return “Par”
golfScore(4, 5) should return “Bogey”
golfScore(4, 6) should return “Double Bogey”
golfScore(4, 7) should return “Go Home!”
golfScore(5, 9) should return “Go Home!”

// tests completed

You didn’t fix the errors I pointed out in my first post.

https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/comparison-with-the-equality-operator/

Ok. Should I change all my = to ===. Kind of confused why some of the tests passed and others didnt.

Some tests passes because your checks for “Hole in One” and “Birdie” are correct.

Here you make a mistake:

else if (strokes = par-1) {
    return names[2];
}

Instead of checking if strokes is equal to par - 1, you’re assigning par - 1 to strokes, since par - 1 is not-zero, that conditional is passing every time. Which means for every other test you’re returning names[2], which is “Birdie”, and that’s why the test that expect “Birdie” also passes.

Instead of assigning par - 1 to strokes, you should compare them by doing this:

else if (strokes === par-1) {
    return names[2];
}

Note that this is not the only error in your code.

1 Like

Thanks very much. I got it to pass now