# JS Basic Algorithm scripting - Title Case a Sentence

Hi,

Apologies in advance for the long post.

I was looking for some feedback on a challenge I recently completed that took me 4 days to eventually solve. Initially, when I started this problem I wasn’t entirely sure where to begin and already opened the ‘Get a Hint’ page to look for some clarification and was even tempted to look at the solutions several times.

I was able to change all characters to lower case, make a copy of the string, and store it in an array then loop through it as stated in the problem explanation section. After eventually figuring out how to access the first character in each element of the array and change it to uppercase. I quickly discovered when logging the array to the console, it was no longer displaying the elements in an array. Each element was being displayed on a new line, I attempted to merge each string using the join() function, but found that the function can only be used on arrays.

Could anyone explain why the copied string array was no longer an array when iterating through the array?
Secondly, following the above is it viable to push the elements into a new array in order to merge each element using the join() function?

Although the challenge is quite small, once I saw all the test cases pass it was a huge accomplishment for me as I tend to suffer from Imposter syndrome.

Please see my code below and my thought process, feel free to criticize.

Thank you for taking the time to read.

function titleCase(str) {
// Your code here...
// Change the word to lowercase before changing it to uppercase.
// Break the string down into its respective words and store it in an array.

var stringArray = str.toLowerCase().split(' ');
// console.log(stringArray);
// Loop through the array.
var newStringArray = [];
for (var i = 0; i < stringArray.length; i++) {
// console.log(stringArray[i])
// We want to access each word through the array.
// In order to access the first letter of each word we must select the zero index.
stringArray[i] =
stringArray[i].replace(stringArray[i][0], stringArray[i][0].toUpperCase());

// After captializing, push all of the original array elements into a new array.
newStringArray.push(stringArray[i]);
}
// Join each word in the array and return the new string.
var mergedString = newStringArray.join(' ');
return mergedString;

}

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

1 Like

I’m not sure what you mean.

Yeah, you can totally do that. I’d either do that or replace the stringArray elements themselves.

Thank you for answering.

In regards to the copied stringArray, I used the .split() function to split the string into an array of substrings, when logging the elements to the console this is the following result:

However, If I log the elements to the console when iterating using a for loop, this is the following result:

Within the for loop block attempting to use the .join() function won’t work on the stringArray and presents the following issue:

TypeError: stringArray[i].join is not a function

If I am not mistaken the .join() function can only be used on arrays, which leads to my question, why is the array that I originally created no longer an array?