DEV Community

Randy Rivera
Randy Rivera

Posted on

Returning The Provided String With The First Letter Of Each Word Capitalized

  • Return the provided string with the first letter of each word capitalized. Make sure the rest of the word is in lower case.
function titleCase(str) {
  return str;
}

titleCase("I'm a little tea pot");
Enter fullscreen mode Exit fullscreen mode
  • Answer:
function titleCase(str) {
  let arrStr = str.toLowerCase().split(" ");
  for (let i = 0; i < arrStr.length; i++) {
    arrStr[i] = arrStr[i][0].toUpperCase() + arrStr[i].slice(1);
    console.log(arrStr[i])
  }
  return arrStr.join(" ");
}

titleCase("I'm a little tea pot"); // will display I'm A Little Tea Pot
Enter fullscreen mode Exit fullscreen mode

Top comments (0)