Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Stuck in a golf game code of a freecodecamp

I am trying to solve the Golf Code challenge in freecodecamp and I can’t really figure out what is wrong with my code here is the direct link. [The link][1] contains the code I am trying to run, just visit the link.

My JS:

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

function golfScore(par, strokes) {
  // Only change code below this line
  var msg=names;
  
  switch(par,strokes){
    case strokes=1:
      msg=names[0];
      break;
    case strokes <= par -2:
      msg=names[1];
      break;
    case strokes=par-1:
      msg=names[2];
      break;
    case strokes=par:
      msg=names[3];
      break;
    case strokes=par+1:
      msg=names[4];
      break;
    case strokes=par+2:
      msg=names[5];
      break;
    case strokes >= par +3:
      msg=names[6];
      break;
  }

  return msg;
  // Only change code above this line
}

golfScore(5, 4);

Requirement:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Passed:golfScore(4, 1) should return the string Hole-in-one!

Failed:golfScore(4, 2) should return the string Eagle

Failed:golfScore(5, 2) should return the string Eagle

Passed:golfScore(4, 3) should return the string Birdie

Passed:golfScore(4, 4) should return the string Par

Passed:golfScore(1, 1) should return the string Hole-in-one!

Passed:golfScore(5, 5) should return the string Par

Passed:golfScore(4, 5) should return the string Bogey

Passed:golfScore(4, 6) should return the string Double Bogey

Failed:golfScore(4, 7) should return the string Go Home!

Failed:golfScore(5, 9) should return the string Go Home!

Strokes Return: 1 "Hole-in-one!"

           <= par - 2         "Eagle"

           par - 1            "Birdie"

           par                "Par"

           par + 1            "Bogey"

           par + 2            "Double Bogey"

           >= par + 3         "Go Home!"

Actually, I found the solutions in ‘if else()’ but, I am trying with ‘switch()’ as well but those bold requirements are not being a success.
[1]: https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-javascript/golf-code

>Solution :

You need to check how switch statement exactly works, I think you misunderstood it. I think the idea of that is to use if-else statements
.

In that case, you might check the cases easily by if and else-if

like

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

function golfScore(par, strokes) {
  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];
}

let result = golfScore(5, 2);
console.log(result);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading