Title Case a Sentence.100

what’s wrong with my code , it is telling me Cannot read property ‘toUpperCase’ of undefined. 4 times.

Your code so far

function titleCase(str) {
  let new223 = str.toLowerCase();
  let new22 = new223.split('');//splits string into array
   new22[0].toUpperCase(); //capitazlizes first letter in the array

    // this loop watches for a space and then captializes the letter just after it (x+1) 
  for(let x in new22){
    if(new22[x] ===" "){   
  new22.join(); //joins the array 
  return new22;

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/69.0.3497.92 Safari/537.36.

Link to the challenge:

So what was written.

Sorry, I should have edited my comment.

Look closely at your logic. I suggest you using a couple console.logs, because you will very easily see your mistake.

The problem here is that you are using the for…in loop which works with objects only.

new22, according to your code, is an array, not an object. Therefore, you should use the usual for loop: for (var i=0; i… etc… (or the while loop, forEach, map…etc.)

changed it man . still not working

could you please give me a hint.

Well, I didn’t promise you that by changing the for in loop with a regular for loop that your code will work.

I just showed you one of the biggest mistakes in your code and it’s up to you to figure out what should be done next.

In fact, this forum doesn’t allow us to post the full answer, but to help people go in the right direction.

right okay , thank you.

You’re welcome.

Good Luck & Happy Coding.

A for in loop will work with this challenge. The catch is, you must remember that i will not be a number, but instead will be a string like. How does it work? Because arrays are really just objects in JavaScript.

@msafieldeen One of the issues caused by the fact that i is a string, is in the following line:

new22[x + 1].toUpperCase();

Because x is a string, the + operator concatenates x and 1. The 1 gets coerced into a string. For example, if i was “0”, then “0” + 1 becomes “01” and new22 does not have a property named “01”, it evaluates to undefined.

The second and third issues with the OP’s code is the following line:


Issue #2) The join method returns a new string and does not mutate the original, so new22 is still an array after this line. The above statement needs to be either returned explicitly or return new22 on the next line.

Issue #3) The join method needs a separator value passed to it. Without specifying one, the string returned from it will be separated by commas by default.

1 Like


I never though that would work with an array. and yeah typeof array is an object. it’s just that I didn’t see that in any challenge that I never used it.

Thank you Mr. @RandellDawson. for the explanations.

wow ,ok that’s alot of issues , is it bad that i want to reset all my code and start all over again. And thanks man

They are small issues which you can easily resolve. I do not think you need to reset. Just read over my suggestions and try to implement. If you get stuck, ask more questions.

so here man i did what you said but changed it to a reglar for loop instead , but seems in console that the logic isn’t even working , it hasn’t capitazlied anything.

    	let new223 = str.toLowerCase();
  		let new22 = new223.split(' ');//splits string into array
  		 new22[0].toUpperCase();  //capitazlizes first letter in the array

    // this loop watches for a space and then captializes the letter just after it (x+1) 
	  for(let x= 0; x<new22.length;x++){
	    if(new22[x]===" "){   
  let joined =  new22.join(' '); //joins the array 
  return joined;

Make sure you use 3 backticks and not 3 single quotes or apostrophes when pasting code. I have fixed it for you.

yes sorry for that…

toUpperCase returns a new string, so you would need to assign it’s returned value to something. It will not directly muate the character located at x+1 index.

so declare a variable and intialize it to


i am really sorry that you have to endure this

You could create another variable and assign it the result of the above, but you could also just assign this above back to the element in new22 which you are trying to replace. See example below.

var arr = ['a', 'b', 'c'];

arr[1] = 'z'

console.log(arr); // displays ['a', 'z', 'c']


var arr = ['a', 'b', 'c'];

arr[1] = arr[1] + 'z'

console.log(arr); // displays ['a', 'bz', 'c']