Files
freecodecamp--freecodecamp/curriculum/challenges/english/blocks/daily-coding-challenges-javascript/68ffb91507a5b645769328c8.md
T
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.9 KiB

id, title, challengeType, dashedName
id title challengeType dashedName
68ffb91507a5b645769328c8 Challenge 105: Character Count 28 challenge-105

--description--

Given a sentence string, return an array with a count of each character in alphabetical order.

  • Treat upper and lowercase letters as the same letter when counting.
  • Ignore numbers, spaces, punctuation, etc.
  • Return the count and letter in the format "letter count". For instance, "a 3".
  • All returned letters should be lowercase.
  • Do not return a count of letters that are not in the given string.

--hints--

countCharacters("hello world") should return ["d 1", "e 1", "h 1", "l 3", "o 2", "r 1", "w 1"].

assert.deepEqual(countCharacters("hello world"), ["d 1", "e 1", "h 1", "l 3", "o 2", "r 1", "w 1"]);

countCharacters("I love coding challenges!") should return ["a 1", "c 2", "d 1", "e 3", "g 2", "h 1", "i 2", "l 3", "n 2", "o 2", "s 1", "v 1"].

assert.deepEqual(countCharacters("I love coding challenges!"), ["a 1", "c 2", "d 1", "e 3", "g 2", "h 1", "i 2", "l 3", "n 2", "o 2", "s 1", "v 1"]);

countCharacters("// TODO: Complete this challenge ASAP!") should return ["a 3", "c 2", "d 1", "e 4", "g 1", "h 2", "i 1", "l 3", "m 1", "n 1", "o 3", "p 2", "s 2", "t 3"].

assert.deepEqual(countCharacters("// TODO: Complete this challenge ASAP!"), ["a 3", "c 2", "d 1", "e 4", "g 1", "h 2", "i 1", "l 3", "m 1", "n 1", "o 3", "p 2", "s 2", "t 3"]);

--seed--

--seed-contents--

function countCharacters(sentence) {

  return sentence;
}

--solutions--

function countCharacters(sentence) {
  const counts = {};

  for (let char of sentence.toLowerCase()) {
    if (char >= "a" && char <= "z") {
      counts[char] = (counts[char] || 0) + 1;
    }
  }

  return Object.keys(counts)
               .sort()
               .map(letter => `${letter} ${counts[letter]}`);
}