# Fundamentals Part 3 - Practice: Are there solutions available?

#1

Hi Guys!

I just did the practice at the end of Fundamentals Part 3, but I feel like my code for the capitalize function is too long.

Are there any solutions available for those exercises, so that I can compare?

If there aren’t existing solutions, I would be very grateful if anyone could have a look at my code, which can be found here:

https://jsfiddle.net/kc1q6209/3/

(The capitalize function is defined from line 31.)

#2

There aren’t any solutions for those… but I can take a look at your code.

function capitalize(str) {
let capRemoved = str.toLowerCase(); //to remove any uppercase letter in str
let firstLetter = str.charAt(0); //to get first letter of str
let capFirstLetter = firstLetter.toUpperCase(); //so that first letter is in uppercase
return capFirstLetter + capRemoved.slice(1);
}

this actually looks pretty good, and is the right method. The only thing that might make it a bit shorter is chaining some of those methods, but in some cases chaining methods only makes the process more confusing and hard to read.

I’d probably do it like this:

function capitalize1(str) {
return str.toUpperCase()[0] + str.toLowerCase().slice(1);
}

but in the end that method is exactly the same as yours… it just smooooshes it all together.

#3

Thank you so much for your help @codyloyd!

Is [0] short for .charAt(0)?

For example, could we write:

function capitalize1(str) {
return str.toUpperCase()[0] + str.toLowerCase().slice(1);
}

I think that, from now on, I will at first write every step --as it may be easier for me to understand–, but then I will try to chain some methods like you did!

#4

ah… with strings, you can access individual characters like you would in an array: string[0] is the first letter.

behind the scenes I don’t think it’s exactly the same as charAt(), but in this case it does the same thing.

#5

Oh I see. It is very useful.