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.5 KiB

id, title, challengeType, dashedName
id title challengeType dashedName
6a2037a68a0bc2aef0006004 Challenge 342: Dice Odds 28 challenge-342

--description--

Given a number of six-sided dice to roll and a target sum, return the odds of rolling that sum as a string in the format "1 in X".

  • The number of dice will be between 1 and 6.
  • The target sum is always achievable with the given number of dice.
  • Round "X" to the nearest whole number.

--hints--

getOdds(1, 5) should return "1 in 6".

assert.equal(getOdds(1, 5), "1 in 6");

getOdds(2, 4) should return "1 in 12".

assert.equal(getOdds(2, 4), "1 in 12");

getOdds(3, 10) should return "1 in 8".

assert.equal(getOdds(3, 10), "1 in 8");

getOdds(4, 7) should return "1 in 65".

assert.equal(getOdds(4, 7), "1 in 65");

getOdds(5, 26) should return "1 in 111".

assert.equal(getOdds(5, 26), "1 in 111");

getOdds(6, 35) should return "1 in 7776".

assert.equal(getOdds(6, 35), "1 in 7776");

--seed--

--seed-contents--

function getOdds(dice, target) {

  return dice;
}

--solutions--

function getOdds(dice, target) {
  let validCombos = 0;
  const totalCombos = Math.pow(6, dice);

  function roll(diceLeft, sum) {
    if (diceLeft === 0) {
      if (sum === target) validCombos++;
      return;
    }
    for (let face = 1; face <= 6; face++) {
      roll(diceLeft - 1, sum + face);
    }
  }

  roll(dice, 0);
  return `1 in ${Math.round(totalCombos / validCombos)}`;
}