DEV Community

Discussion on: Challenge-- Capitalize each word in a string

Collapse
 
cyberhck profile image
Nishchal Gautam • Edited

This is mixing functional, I find it a bit weird, what I'd do:

function capitalize(str) {

  // Split string into an array of words
  const string = str.split(" ");

  // Convert every first letter to uppercase
  return string.map((word) => {
    return word[0].toUpperCase() + word.slice(1).toLowerCase();
  }).join(" ");
}

I'd also move the capitalize first letter to separate function to gain some performance