Title Case a Sentence.100

Title Case a Sentence.100
0

#1

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


#3

So what was written.


#4

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.


#5

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.)


#6

changed it man . still not working


#7

could you please give me a hint.


#8

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.


#9

right okay , thank you.


#10

You’re welcome.

Good Luck & Happy Coding.


#11

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:

new22.join();

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.


#12

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. @RandellDawson. for the explanations.


#13

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


#14

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.


#15

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;
 }

#16

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


#17

yes sorry for that…


#18

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.


#19

so declare a variable and intialize it to

new22[x+1].toUpperCase();


i am really sorry that you have to endure this


#20

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']

OR

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

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

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

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

???