freeCodeCamp/curriculum/challenges/english/02-javascript-algorithms-and-data-structures/basic-algorithm-scripting/title-case-a-sentence.md
Anna 6fa282cb6f
Some checks failed
i18n - Build Validation / Validate i18n Builds (20.x) (push) Has been cancelled
CI - Node.js / Lint (20.x) (push) Has been cancelled
CI - Node.js / Build (20.x) (push) Has been cancelled
CI - Node.js / Test (20.x) (push) Has been cancelled
CI - Node.js / Test - Upcoming Changes (20.x) (push) Has been cancelled
CI - Node.js / Test - i18n (italian, 20.x) (push) Has been cancelled
CI - Node.js / Test - i18n (portuguese, 20.x) (push) Has been cancelled
i18n - Download Client UI / Client (push) Has been cancelled
chore(curriculum): update asserts in legacy basic algorithm scripting (#57784)
Co-authored-by: Naomi <accounts+github@nhcarrigan.com>
2025-01-04 13:10:00 +01:00

1.4 KiB

id title challengeType forumTopicId dashedName
ab6137d4e35944e21037b769 Title Case a Sentence 1 16088 title-case-a-sentence

--description--

Return the provided string with the first letter of each word capitalized. Make sure the rest of the word is in lower case.

For the purpose of this exercise, you should also capitalize connecting words like the and of.

--hints--

titleCase("I'm a little tea pot") should return a string.

assert.isString(titleCase("I'm a little tea pot"));

titleCase("I'm a little tea pot") should return the string I'm A Little Tea Pot.

assert.strictEqual(titleCase("I'm a little tea pot"), "I'm A Little Tea Pot");

titleCase("sHoRt AnD sToUt") should return the string Short And Stout.

assert.strictEqual(titleCase('sHoRt AnD sToUt'), 'Short And Stout');

titleCase("HERE IS MY HANDLE HERE IS MY SPOUT") should return the string Here Is My Handle Here Is My Spout.

assert.strictEqual(
  titleCase('HERE IS MY HANDLE HERE IS MY SPOUT'),
  'Here Is My Handle Here Is My Spout'
);

--seed--

--seed-contents--

function titleCase(str) {
  return str;
}

titleCase("I'm a little tea pot");

--solutions--

function titleCase(str) {
  return str
    .split(' ')
    .map(word => word.charAt(0).toUpperCase() + word.substring(1).toLowerCase())
    .join(' ');
}

titleCase("I'm a little tea pot");