Title Case a Sentence--gt

Tell us what’s happening:
I am getting the desired output , but unable to clear the challenge, I know its completely different than expected solutions and is much longer because I have not used those advanced functions and just basic conditionals , but not sure why it does not satisfy the requirement.

Your code so far


function titleCase(str) {
  let sp = str.split(" ")
  let p=[];
  let p1=[];
  // using for loop to convert all elements in an array to lower case and split them individually and store them in array p.
  for (let i = 0 ; i < sp.length; i ++){
    p=sp[i].toLowerCase().split("");
  // using for loop to convert first letter of the individual element to upper case and push them into an array p1.
    for(let j=0;j<p.length;j++){
     if  ((j ==0) && (j == (p.length)-1) ){
       p1.push(p[j].toUpperCase()+ " ")
     }
     else if ( j== p.length-1){
       p1.push(p[j]+ " ")
     }     
     
     else if (j == 0){
       p1.push(p[j].toUpperCase())  
     }
     else p1.push(p[j])
      }
    
    }
    
    return p1.join("") 
  }
 
 


console.log(titleCase("I'm A Little Tea Pot"));

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36.

Link to the challenge:

well… you may have an extra space at the end.

I have added the arrows to mark start and end of the string, note the space before the arrow to the right:
image

1 Like

As ieahleen mentioned, it appears you’re not calling the trim function before returning the final string value.

In case you want to review a solution that does not require for-loops and uses the functions you mentioned in your question:

function titleCase(str) {
  // Call toLowerCase() on the string (str) argument, so you only do this once, instead of inside the loop
  // split() the sentence into an array of words
  // then use the map to iterate over the array and transform each word
  return str.toLowerCase().split(" ").map(word=>{
   // As we map over each word in the sentence, create an array from the word
   // Use Array destructing to extract the first letter
   // If the word is 'little' Array.from(word) will produce [l,i,t,t,l,e]
   // firstLetter = "l" and ...remainingLettersArray = [i,t,t,l,e]
    const [firstLetter, ...remainingLettersArray] = Array.from(word);
    // Now we'll transform firstLetter to upper-case and call join() on the [i,t,t,l,e] array.
   // Finally, return "Little" to the map function call.
    return firstLetter.toUpperCase() + remainingLettersArray.join("");
}).join(" ");
 // When the map function completes for each item in the sentence you will have an array that looks like
 // ["I'm", "A", "Little", "Tea", "Pot"]
 // So, make sure to call join() (above) on the result array to produce the string in title case
}
// Solution without comments
function titleCase(str) {
  return str.toLowerCase().split(" ").map(word=>{
    const [firstLetter, ...remainingLettersArray] = Array.from(word);
    return firstLetter.toUpperCase() + remainingLettersArray.join("");
}).join(" ");
}

Here are some links to the documentation for each of the array methods I’ve used in the code above:

Array.prototype.map()

Array.from()

Destructuring assignment

And here is a video from Wes Bos, in-case you want some practice with array/object destructuring: https://www.youtube.com/watch?v=_ApRMRGI-6g

1 Like

Thanks i used trim function to take space out and it worked. Thanks your help.

Thank you so much about your explanation and suggestion to use trim method.I was able to understand the map function better and spread operator as well. Thank you so much for detailed comments. Cheers!

1 Like