0

In the string str I am making the string lower case and capitalizing the first word. Is there a better way to modify strings then what I did? I tried doing return str.toLowerCase().charAt(0).toUpperCase();, however, all this does is return the letter H. Is there a better way to chain methods onto a variable containing a string?

let str = `HOW ARE YOU DOING TODAY?`

function capitalize() {

  str1 = str.toLowerCase()

  return str.charAt(0).toUpperCase() + str1.slice(1);
}

console.log(capitalize());  /// -> How are you doing today?
5
  • 3
    Anything wrong with str.charAt(0).toUpperCase() + str1.slice(1) in particular? Commented Jul 9, 2020 at 3:00
  • simply use css text-transform: capitalize; ? Commented Jul 9, 2020 at 3:07
  • I was hoping there was a way to do something along the lines of str.toLowerCase().charAt(0).toUpperCase() rather then dissecting the sentence into parts. Commented Jul 9, 2020 at 3:09
  • either of the comments above do what you are asking without dissecting the sentence into parts. Commented Jul 9, 2020 at 3:11
  • I am having to select the first character of the string and then slice the remaining part of the sentence and add them together. I was trying to ask if you could modify the string with methods without having to concatenate the 'divided parts' together. Commented Jul 9, 2020 at 3:18

4 Answers 4

1

Your concept and the implementation approach is correct. You can use the template literals and arrow functions, to make the code more concise

const capitalize = (str) =>  `${str.charAt(0).toUpperCase()}${str.slice(1).toLowerCase()}`;
Sign up to request clarification or add additional context in comments.

1 Comment

Template literals in this particular case make the expression several characters longer.
1

Here's a simple elegant solution:

let str = "HOW ARE YOU DOING TODAY?";

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

console.log(capitalize(str));

Comments

1

let str = `HOW ARE YOU DOING TODAY?`

function capitalize(str) {

  return `${str[0].toUpperCase()}${str.slice(1).toLowerCase()}`;
}

console.log(capitalize(str));  /// -> How are you doing today?

Comments

0

As strings are immutable in JS, you can't change them in place. You could instead consider using .replace() with a regular expression to obtain the first character (.) and the rest (.+) so that you can make the first character uppercase, and the rest lowercase:

const capitalize = str =>
  str.toLowerCase().replace(/(.)(.+)/g, (_, f, r) => f.toUpperCase() + r);

console.log(capitalize("HOW ARE YOU DOING TODAY?"));

You could also consider using destructuring to obtain the first character and rest of the string like so:

const capitalize = ([f="", ...r]) =>
  `${f.toUpperCase()}${r.join('').toLowerCase()}`;

console.log(capitalize(""));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.