Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Trevor Johnson
14,427 PointsJS Title Case A Sentence
This challenge is asking me to capitalize the first letter of each word of the passed in argument. When my code runs it only returns the last word with the first letter capitalized. It seems like it is running through the loop and capitalizing the first letter on each word, but it is not returning the full string. I appreciate your help.
Please try to not post the exact answer, but provide hints that would help me come to the solution. I know that can be hard sometimes, but try your best.
function titleCase(str) {
var array = str.toLowerCase().split(' ');
var final;
var cap;
for (i = 0; i < array.length; i ++) {
for (j = 0; j < array[i].length; j ++) {
if (j === 0) {
cap = array[i][j].toUpperCase();
final = array[i].replace(array[i].charAt(0), cap);
}
}
}
return final;
}
titleCase("I'm a little tea pot");
Vlad Legkowski
9,882 PointsThis is what i did
function titleCase(str) {
var array = str.toLowerCase().split(' ');
var final = [];
var cap;
for (i = 0; i < array.length; i ++) {
for (j = 0; j < array[i].length; j ++) {
if (j === 0) {
cap = array[i][j].toUpperCase();
final.push(cap);
}
}
}
return final.join('');
}
3 Answers
Vlad Legkowski
9,882 PointsHi Trev,
I would push each capitalized letter into an array, and then simply would Array.prototype.join() the array with individual letters on your return
Have a look here at the examples what separator to use
Lmk if you want me to post the code
Trevor Johnson
14,427 PointsThanks Vlad, the array.join() method helped a lot.
Umesh Ravji
42,386 PointsHi Trevor, this is what I think is the most straight-forward approach to this, but you can do a one-liner with the array map() method if your feeling keen :)
- Create an array of all the string values (as you have).
- Iterate over all of the strings, modify, then append them to a temporary array for storage. There's no need for nested loops here, a single loop is enough.
- Join all the changed values in the temporary array back into a string before returning.
When capitalizing the words, you may want to look at using the substring() method, in combination with the charAt() method you are already using.
miikis
44,957 PointsHey Trevor,
The best way to eat an elephant is one bite at a time. The problem you present is hardly an elephant but you could still benefit by breaking the problem down into more manageable pieces and systematically solving your way back up to the original problem.
In this context, you'll want to first decide what sort of input your titleCase(string) function can take. It'd probably be safe to assume the input's data type is String: a sentence or a word. From there, you'll want to pick a method of separating a sentence into words. The path of least resistance here is to separate by spaces but if you're feeling adventurous you can write your own Regular Expression as well. Regardless of your chosen method of separation, JavaScript will already do the heavy lifting for you by means of the String#split(separator) function. Still, your code will be more readable if you split this functionality out into its own function. You could call it separateString(string).
Now that you have an array of words, you'll want to cycle through each word and capitalize it. Before that, however, you'll need to figure out how to capitalize a given word. This is a great opportunity to create another function for this explicit purpose; you could call it capitalize(string). The algorithm for capitalizing a given word is pretty straightforward. You grab the first letter of the word and you String#toUpperCase() it. Then, you grab the rest of the word with String#substr(index) and you String#toLowerCase that. And, finally you concatenate those two parts and return them from capitalize(string).
Well, so far you have created a means of splitting a given sentence into an array of words and a means of capitalizing a given word. The only responsibility left for your titleCase(string) function is putting the parts together. As luck would have it, JavaScript has a method ready for just this purpose: Array#map(yourFunctionHere). Because of this method, all titleCase(string) would have to do is feed the string it was given into separateString(string) to get an array of words; call Array#map(capitalize) on that array of words; use Array#join(separator) to join the subsequent output (using the same method of separation you used earlier); and, finally, return the output.
So, yeah, that's how you'd do it. Let me know if you have any questions :)
Trevor Johnson
14,427 PointsTrevor Johnson
14,427 PointsThanks a lot. I figured it out. The map method was so much easier. By the way, could one of you guys tell me how you change the font of a JavaScript method, such as map() or charAt() when you are simply typing like this?