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[x+1].toUpperCase();
    }
  }
  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:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-algorithm-scripting/title-case-a-sentence

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.

Wow,

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. @camperextraordinaire. 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

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]===" "){   
	      new22[x+1].toUpperCase();
	    }
	  }
  
  let joined =  new22.join(' '); //joins the array 
  return joined;
 }

yes sorry for that…

so declare a variable and intialize it to

new22[x+1].toUpperCase();


i am really sorry that you have to endure this

new22[x+1]=new22[x+1].toUpperCase();

???

Nothing new…

actually i figured something out , so new22[x+1] for example works on a complete word not it’s first charcter

1 Like

Thanks for putting up with me though man , i will try again in 15mins.
But also wanted to ask , when you write bad code , do you just go with to learn or do you abandon it.

Yeah , that’s it …

Right , thanks man for your help.