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

id, title, challengeType, dashedName
id title challengeType dashedName
69272dcf1c24b44fd79137c5 Challenge 142: Sum the String 28 challenge-142

--description--

Given a string containing digits and other characters, return the sum of all numbers in the string.

  • Treat consecutive digits as a single number. For example, "13" counts as 13, not 1 + 3.
  • Ignore any non-digit characters.

--hints--

stringSum("3apples2bananas") should return 5.

assert.equal(stringSum("3apples2bananas"), 5);

stringSum("10cats5dogs2birds") should return 17.

assert.equal(stringSum("10cats5dogs2birds"), 17);

stringSum("125344") should return 125344.

assert.equal(stringSum("125344"), 125344);

stringSum("a1b20c300") should return 321.

assert.equal(stringSum("a1b20c300"), 321);

stringSum("a12b34c56d78e90f123g456h789i0j1k2l3m4n5") should return 1653.

assert.equal(stringSum("a12b34c56d78e90f123g456h789i0j1k2l3m4n5"), 1653);

--seed--

--seed-contents--

function stringSum(str) {

  return str;
}

--solutions--

function stringSum(str) {
  const matches = str.match(/\d+/g); // find all consecutive digits
  return matches.reduce((sum, num) => sum + Number(num), 0);
}