sajad torkamani

You want a function that takes a string and returns the string with the first character in uppercase and the rest in lowercase:

capitalize('john doe') // John Doe
capitalize('JOHN DOE') // John Doe

Here’s how in JavaScript:

function capitalize(str) {
  return str.slice(0, 1).toUpperCase() + str.slice(1).toLowerCase()
}

repl

Tagged: Algorithms