String Formatter

Normalize names with string cleanup and formatting functions.

Start building, submit solution and get feedback from the community.
2Submit Solution
5 upvotes10 upvotes

String Formatter

A signup form is receiving names with inconsistent spacing and casing. Normalize the names before showing them back to the user.

Write these functions:

  • cleanText(text) should remove spaces from the start and end of a string.

  • capitalize(text) should uppercase the first character and lowercase the rest.

  • formatDisplayName(firstName, lastName) should clean and capitalize both names, then return the full display name.

Sample checks:

js
console.log(formatDisplayName('  ava', 'STONE  '));console.log(formatDisplayName('nOAh', '  kim'));console.log(formatDisplayName('  mINA  ', 'pATEL'));

Expected output:

txt
Ava StoneNoah KimMina Patel

formatDisplayName should reuse the smaller helpers instead of repeating all the string cleanup work.

Solution

Solution:

js
function cleanText(text) {  return text.trim();}function capitalize(text) {  const cleaned = cleanText(text).toLowerCase();  if (cleaned.length === 0) {    return '';  }  return cleaned[0].toUpperCase() + cleaned.slice(1);}function formatDisplayName(firstName, lastName) {  const cleanFirstName = capitalize(firstName);  const cleanLastName = capitalize(lastName);  return `${cleanFirstName} ${cleanLastName}`;}console.log(formatDisplayName('  ava', 'STONE  '));console.log(formatDisplayName('nOAh', '  kim'));console.log(formatDisplayName('  mINA  ', 'pATEL'));

Join the Community

roadmap.sh is the 6th most starred project on GitHub and is visited by hundreds of thousands of developers every month.

Rank  out of 28M!

357K

GitHub Stars

Star us on GitHub
Help us reach #1

+90kevery month

+2.8M

Registered Users

Register yourself
Commit to your growth

+2kevery month

48K

Discord Members

Join on Discord
Join the community

RoadmapsGuidesFAQsYouTube

roadmap.shby@nilbuild

Community created roadmaps, best practices, projects, articles, resources and journeys to help you choose your path and grow in your career.

© roadmap.sh·Terms·Privacy·

ThewNewStack

The top DevOps resource for Kubernetes, cloud-native computing, and large-scale development and deployment.