Title Case a Sentence - Showing Capitalised on console.log but how do i join it together?

Hello,

For this algorithm, I use console.log to show the output of the capitalisation. This works and it shows all the beginning letters capitalised but how do I get this into an array and join?

Your code so far


function titleCase(str) {

var arr = str.split(' ') 
for (var i = 0; i < str.length; i++ ) {
 var capitalized = console.log(arr[i].charAt(0).toUpperCase() + arr[i].slice(1))
}
return console.log(capitalized)
}

titleCase("I'm a little tea pot");

Don’t wrap those parts of code in console.log. Just do a plain assignment:

var capitalized = arr[i]...

If you need to print, you can then use the variable right on the next line:

var capitalized = arr[i]...
console.log(capitalized);

Okay, its giving me the same output. How can I join the output together as a string?

You might want to put a new variable before the for-loop, where’ you’ll accumulate the for-loop’s results. It could be an array. Then for each loop, you could push the capitalized word to that array. After the for-loop, you can then use .join(' ') on that array.

Do you mean like this?
The console.log just gives me
1
2
3
4
5
running tests gives me:
Cannot read property ‘charAt’ of undefined


function titleCase(str) {

var arr = str.split(' ')

var capArray = [];

for (var i = 0; i < str.length; i++ ) {

var capitalized = arr[i].charAt(0).toUpperCase() + arr[i].slice(1);

var updatedSentence = capArray.push(capitalized);

console.log(updatedSentence)

}

return updatedSentence.join("  ")

}

titleCase("I'm a little tea pot");

Since it’s arr you’re accessing in the for-loop, you probably should use arr.length, not str.length. Right now your loop is looping way beyond arr's length, so arr[i] is undefined.

There’s no need to assign the .push() to the updatedSentence variable. It just contains the new length of the array after pushing (hence the numbers 1 to 5).

Your join also has two spaces in it.

function titleCase(str) {

var arr = str.split(' ')

var capArray = [];

for (var i = 0; i < arr.length; i++ ) {

 capArray = arr[i].charAt(0).toUpperCase() + arr[i].slice(1);

}

return capArray.join(" ")

}

titleCase("I'm a little tea pot");

You already got that part right in your previous code! Just push capitalized to capArray, but there’s no need to assign it to a variable.

Okay so now the console.log() is showing me the correct output i.e. joined but when I use return capArray.join() it doesnt work.

function titleCase(str) {

var arr = str.split(' ')

var capArray = [];

for (var i = 0; i < str.length; i++ ) {

var capitalized = arr[i].charAt(0).toUpperCase() + arr[i].slice(1);

capArray.push(capitalized);

console.log(capArray.join(' '));

}

return 
}

titleCase("I'm a little tea pot");

Why did you move capArray.join out of the return statement? Also you’re using str.length again. What exactly doesn’t work? Which tests pass/don’t pass?

I don’t mean to pressure, but you were really close to solving it.

Some hints

Move the capArray.join bit back to the return statement, and fix back arr.length. Only two tests will pass. Now try running the inputs from the failing tests. Compare your output with the expected result. Then think about how you could fix it.

If you need to see your output, use console.log(titleCase('your input string here...'))

I got it! Thank you so much. I think I was just trying to rush :sweat_smile: