Files
wehub-resource-sync dde272c4b8
i18n - Build Validation / Validate i18n Builds (24) (push) Has been cancelled
CI - Node.js / Lint (24) (push) Has been cancelled
CI - Node.js / Build (24) (push) Has been cancelled
CI - Node.js / Test (24) (push) Has been cancelled
CI - Node.js / Test - Upcoming Changes (24) (push) Has been cancelled
CI - Node.js / Test - i18n (italian, 24) (push) Has been cancelled
CI - Node.js / Test - i18n (portuguese, 24) (push) Has been cancelled
CD - Docker - GHCR Images / Build and Push Images (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 11:55:53 +08:00

1.6 KiB

id, title, challengeType, dashedName
id title challengeType dashedName
6a26df3e2988bcdded204895 Challenge 359: Golf Handicap Calculator 28 challenge-359

--description--

Given an array of golf scores and a corresponding array of course par values, return the golfer's handicap index using the following method:

  • Calculate the differential for each round by subtracting the par from the score, then return the average of all differentials rounded to one decimal place.

--hints--

calculateHandicap([72, 72, 72], [72, 72, 72]) should return 0.

assert.equal(calculateHandicap([72, 72, 72], [72, 72, 72]), 0);

calculateHandicap([80, 76, 78, 78], [72, 72, 72, 72]) should return 6.

assert.equal(calculateHandicap([80, 76, 78, 78], [72, 72, 72, 72]), 6);

calculateHandicap([42, 45, 46, 44], [36, 36, 36, 36]) should return 8.3.

assert.equal(calculateHandicap([42, 45, 46, 44], [36, 36, 36, 36]), 8.3);

calculateHandicap([85, 80, 76, 79, 82], [72, 72, 72, 71, 71]) should return 8.8.

assert.equal(calculateHandicap([85, 80, 76, 79, 82], [72, 72, 72, 71, 71]), 8.8);

calculateHandicap([41, 50, 48, 52, 46, 49], [35, 37, 35, 37, 35, 37]) should return 11.7.

assert.equal(calculateHandicap([41, 50, 48, 52, 46, 49], [35, 37, 35, 37, 35, 37]), 11.7);

--seed--

--seed-contents--

function calculateHandicap(scores, pars) {

  return scores;
}

--solutions--

function calculateHandicap(scores, pars) {
  const differentials = scores.map((score, i) => score - pars[i]);
  const average = differentials.reduce((sum, d) => sum + d, 0) / differentials.length;
  return Math.round(average * 10) / 10;
}