FreeCodeCamp-JavaScript Algorithms and Data Structures

Records:
Golf Code
In the game of Golf, each hole has a par, meaning, the average number of strokes a golfer is expected to make in order to sink the ball in the hole to complete the play. Depending on how far above or below par your strokes are, there is a different nickname.

Your function will be passed par and strokes arguments. Return the correct string according to this table which lists the strokes in order of priority; top (highest) to bottom (lowest):

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!”

par and strokes will always be numeric and positive. We have added an array of all the names for your convenience.

Instance Code

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

function golfScore(par, strokes) {
  const length = names.length-1;
  const arerage = length / 2;
  if(par == strokes) {
    if(strokes <= 1){
      return names[0];
    }
    return names[arerage];
  }else if(par > strokes && par < length){
      return names[strokes - 1];
  }else {
    const index = strokes - par + arerage;
    return index < length ? names[index] : names[length];
  }
}

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!

你可能感兴趣的:(FreeCodeCamp,javascript,开发语言,ecmascript,FreeCodeCamp,算法)