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

2.2 KiB

id, title, challengeType, dashedName
id title challengeType dashedName
698a1a73ade5ac0e19180fa3 Challenge 200: Letter and Number Count 28 challenge-200

--description--

Given a string, return a message with the count of how many letters and numbers it contains.

  • Letters are A-Z and a-z.
  • Numbers are 0-9.
  • Ignore all other characters.

Return "The string has X letters and Y numbers.", where "X" is the count of letters and "Y" is the count of numbers. If either count is 1, use the singular form for that item. E.g: "1 letter" instead of "1 letters" and "1 number" instead of "1 numbers".

--hints--

countLettersAndNumbers("helloworld123") should return "The string has 10 letters and 3 numbers.".

assert.equal(countLettersAndNumbers("helloworld123"), "The string has 10 letters and 3 numbers.");

countLettersAndNumbers("Catch 22") should return "The string has 5 letters and 2 numbers.".

assert.equal(countLettersAndNumbers("Catch 22"), "The string has 5 letters and 2 numbers.");

countLettersAndNumbers("A1!") should return "The string has 1 letter and 1 number.".

assert.equal(countLettersAndNumbers("A1!"), "The string has 1 letter and 1 number.");

countLettersAndNumbers("12345") should return "The string has 0 letters and 5 numbers.".

assert.equal(countLettersAndNumbers("12345"), "The string has 0 letters and 5 numbers.");

countLettersAndNumbers("password") should return "The string has 8 letters and 0 numbers.".

assert.equal(countLettersAndNumbers("password"), "The string has 8 letters and 0 numbers.");

--seed--

--seed-contents--

function countLettersAndNumbers(str) {

  return str;
}

--solutions--

function countLettersAndNumbers(str) {
  let letterCount = 0;
  let numberCount = 0;

  for (let char of str) {
    if ((char >= 'A' && char <= 'Z') || (char >= 'a' && char <= 'z')) {
      letterCount++;
    } else if (char >= '0' && char <= '9') {
      numberCount++;
    }
  }

  const letterWord = letterCount === 1 ? "letter" : "letters";
  const numberWord = numberCount === 1 ? "number" : "numbers";

  return `The string has ${letterCount} ${letterWord} and ${numberCount} ${numberWord}.`;
}