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):

StrokesReturn
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!
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值