Title Case a Sentence - var inside for loop?

Title Case a Sentence - var inside for loop?
0.0 0

#1

Hello, I’m trying to determine how to return the only the capital letters from the array of words. I have created the for loop , iterating through each word with the intent to capture the first letter of each using newStr[i].charAt(0).toUpperCase(); however, I’m running into problems with what to actually do inside the for loop. Would I need to create another variable here and push the capital letters into another array?

I attempted to use newStr+=newStr[i].charAt(0).toUpperCase(); but that only caused an infinite loop(I’m assuming because it was writing over newStr). I’m really baffled by this and know it’s something super easy,. If anyone could help w/o revealing any code it would be awesome and I’d owe you a sandwich.

Your code so far

  
var newStr=str.toLowerCase().split(" ");
  
    for(i=0;i<newStr.length;i++){
      newStr[i].charAt(0).toUpperCase();
      
    }return newStr;
}

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

Your browser information:

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

Link to the challenge:


#2

The above line does not really accomplish anything, because you need to assign this code segment to something. I assume you want to replace the contents of newStr[i]? If so, assign this code segment to newStr[i].

Keep in mind, that if you do what I suggest, then when you return newStr, it is only going to be an array of capital letters for elements.

titleCase(“I’m a little tea pot”) would return [“I”, “A”, “L”, “T”, “P”]


#3

Thanks Randell! The missing piece for me was setting newStr[i] as the variable, like this:

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

Although I don’t quite understand why setting newStr=`newStr[i].charAt(0).toUpperCase() + newStr[i].slice(1); inside the loop wouldn’t accomplish that, and instead creates an infinite loop. Is it because when the variable is set inside the for loop the entire str is being written over?

I did achieve the answer, so thank you again!

function titleCase(str) {

var newStr=str.toLowerCase().split(" ");

for(i=0;i<newStr.length;i++){
  newStr[i]=newStr[i].charAt(0).toUpperCase() + newStr[i].slice(1);
  
}return newStr.join(" ");

}


#4

It is because your for loop condition is based on the length of newStr and since the loop keeps going as long as I is less than newStr, it will never stop, because you keep concatenating something to newStr.


#5

I see, but why does an infinite loop occur when I set newStr as the variable instead of newStr[i]? That’s what kind of puzzles me.


#6

Your for loop keeps increasing the length of newStr, so how could i ever be greater or equal to newStr.length?


#7

I think I’m beginning to understand, but even without concatenating str inside the for loop it would still generate an infinite loop. The length of newStr would stay the same, correct, if we aren’t concatenating anything?


#8

Test your theory out with some code.


#9

Yep, that’s exactly what it was doing, still adding to the length on each iteration. Thanks again for all your help!