Tell us what’s happening:
I’m not quite sure why the test isn’t passing since the console.log is showing the correct solution.
I’ve commented my code to the best of my beginner knowledge Your code so far
//Title Case a Sentence
//return string with first letter of each word capitalized
function titleCase(str) {
let arrStr=str.toLowerCase().split(" "); //converts string str to lowercase letters
//splits str into array elements by the spaces between each word " "
str= ''; //empty string
//iterates through each element within the array
for (let i=0; i<arrStr.length; i++) {
arrStr[i] = arrStr[i].charAt(0).toUpperCase() + arrStr[i].slice(1); //syntax: string.charAt(index) returns the character at the specified index in a string.
//syntax string.toUpperCase() converts string characters to upper case
//syntax string.slice(1) copies a given number of elements/characters into a new array/string. since only one parameter was given (1), it copies from 1st position to the end of the string
str += arrStr[i] + ' '; //str= str + arrStr[i] + ' '
//combines array elements back into one string with a space bettween each words ' '
}
return str;
}
console.log(titleCase("I'm a little tea pot")); //logs I'm a Little Tea Pot
console.log(titleCase("sHoRt AnD sToUt")); //logs Short And Stout
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.89 Safari/537.36.
Got it to work with the join method. solution as follows:
function titleCase(str) {
let arrStr=str.toLowerCase().split(" "); //converts string str to lowercase letters
//splits str into array elements by the spaces between each word " "
//iterates through each element within the array
for (let i=0; i<arrStr.length; i++) {
arrStr[i] = arrStr[i].charAt(0).toUpperCase() + arrStr[i].slice(1); //syntax: string.charAt(index) returns the character at the specified index in a string.
//syntax string.toUpperCase() converts string characters to upper case
//syntax string.slice(1) copies a given number of elements/characters into a new array/string. since only one parameter was given (1), it copies from 1st position to the end of the string
}
str= arrStr.join(" "); //syntax: array.join(separator). separator is a space between words " "
// .join() method must be set to a variable
return str;
}
console.log(titleCase("I'm a little tea pot")); //logs I'm a Little Tea Pot