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;
}