Title Case a Sentence, regex .shift .push

Tell us what’s happening:
I’ve put in several hours on this and have gone through many ideas. I understand the logic of the 3 solutions but I really want to try and finish this one-or at least solve the problems I am having. First Idea was to perform a series of operations and update the var str each time a function is applied to it. var Uno// equal to array (i/'m,a,little,tea,pot)was going to be separated into two steps. The problem I am having is undefined variables: console.log(Uno)//undefined,
var str = "I'm a little tea pot"; //ReferenceError: str is not defined . Also was wondering if var regex = /^\w+/g; could be a way to match the[0] char then use .shift, .push instead of .remove. any advice is much appreciated.

Your code so far


function titleCase(str) {
var Uno = str.toLowerCase.split(" ");
return Uno;
}

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

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.1; ) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36.

Challenge: Title Case a Sentence

Link to the challenge:

I do not see the above in the code you posted. Make sure to post all the code you have tried.

Ok,
I was anxious about doing that because its like spaghetti all over the walls…

Here is what I am working on now…

function titleCase(str) {
var Uno = str.split(" ");

  for (i = 0; i < str.length; i++) {
Uno[i].toLowerCase().split('');
Uno[i][0] = str[i][0].toUpperCase;
Uno[i] = str[i].join("");}
return Uno;
}
console.log(Uno);// ReferenceError: Uno is not defined

Uno is only defined inside the function.

1 Like

Also, you are iterating through str here and referencing an i related to str, but you are trying to use i with Uno?

What are you hoping to accomplish in these two lines?

ok so I can’t call a console.log on a var declared inside the function? The length should be connected to the Uno variable. Originally, I was experimenting with using a new variable at each stage (Uno Dos Tres). I was rewriting this away from the previous Idea. the I iterates through the array to lower the case…It also seemed like a good way to access the [0] character to substitute upper case.
…the console.log is what I am using to see where I am going wrong and if it fails the test I want to know why.

@magopolis Variables declared inside a function are only available for reference inside that function. Your console.log statement is outside the function.

1 Like

Thats actually a really big help. Thanx
M

var uno = str.toLowerCase();
var tres = "";
var dos = uno.split(" ");
for (var i = 0; i < dos.length; i++)
for (var a = 0; a < dos.length; a++)

{tres = dos[i][a].replace(dos[i].charAt[i][0], L => L.toUpperCase());}
return tres;}

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

So I have it down to one last step and it is backfiring in numerous ways. I tried pushing tres to a var result. I got “pot”, “Pot” and P as returns. I get 0 for a, 5 for var i. if a = 0, maybe that explain “P”. I’m searching the web for charAt() with nested arrays to figure out how to access them and form the final string. Any ideas on how to get there?
MP