Title Case a Sentence - "not a function"

Tell us what’s happening:

Keep getting errors of the nature “such and such is not a function”. Right now it’s telling me charArray.join("") is not a function, but no matter how I try to work around it, I always end up banging against something not being a function and I’m not sure what to make of it, as I’ve checked my syntax (I think) and I’m following the way these are implemented in the literature.

Your code so far

function titleCase(str) {
  
  str = str.toLowerCase();
  
  var wordArray = str.split(" ");
  
  for (x = 0; x < wordArray.length; x++) {
    
    var charArray = wordArray[x].split("");
    charArray = charArray[0].toUpperCase;    
    charArray.join("");
    
    
    
    
//     var firstLetter = testArray[x].shift;
//     firstLetter.toUppercase();
    
    
//     testArray = testArray[x].makeUppercase[0];
  }

  return wordArray;
}

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

Your browser information:

Your Browser User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36.

Link to the challenge:
https://www.freecodecamp.org/challenges/title-case-a-sentence

1 Like

You’re assigning the toUpperCase function itself to charArray. Maybe you meant charArray[0].toUpperCase() ?

If you added the () at the end, it will then assign the uppercased char to the charArray variable itself. It replaces the array with a string, and strings don’t have a join method.

This returns a brand new string which you have to assign somewhere.

1 Like

You should be careful with variable reassignment:

let’s look at this portion of your code:

    var charArray = wordArray[x].split("");
    charArray = charArray[0].toUpperCase;    
    charArray.join("");

Let’s start with the first line: your are telling that charArray is an array composed by each individual letter of the word being look upon.
For example

var charArray = wordArray[x].split("");
//[ 'l', 'i', 't', 't', 'l', 'e' ]

On the very next line however you are telling that charArray is now the first value of the said array but uppercase.
NOTE: I think you forgot to call the function, instead you were assigning it to the function itself.

charArray = charArray[0].toUpperCase(); // <-- call the function  
// now charArray is just 'L' 

Finally you are trying to join `charArray, which is now a single letter and not an array anymore, thus is not a valid function.

Hope it makes sense :slight_smile:

1 Like

Ahhhh! That’s totally the problem then. So would the solution be to just run the function and not assign the variable?

Stuck on a new issue - charArray[0].toUpperCase isn’t working as intended. Any ideas?


function titleCase(str) {
  
  str = str.toLowerCase();
  
  var wordArray = str.split(" ");
  
  for (x = 0; x < wordArray.length; x++) {
    
    var charArray = wordArray[x].split("");
    charArray[0].toUpperCase();    
    wordArray[x] = charArray.join("");
    
  }
  
  return wordArray.join(" ");
}

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

toUpperCase() (or any other string method) doesn’t mutate the string they work with. You need to assign it somewhere.

1 Like

OK, this is absolutely driving me nuts. Now I’m doing:

  for (x = 0; x < wordArray.length; x++) {
    
    var charArray = wordArray[x].split("");
    var firstLetter = charArray[0].toUpperCase();   
    charArray = charArray.shift;
    charArray = charArray.unshift(firstLetter);
    wordArray[x] = charArray.join("");
    
  }

Again getting unshift is not a function. I’m assigning the toUppercase to a variable, because I have no idea what else to do, then I’m doing this ridiculous taking away the old lowercase first letter and unshifting the new variable on there, and again totally at a loss.

You’re previous code was already close to a passing solution! I said that you need to assign the output of toUpperCase() somewhere but nothing’s stopping you from assigning it to the same string!

shifting and unshifting could work, but there’s a few things to note. These functions mutate the array they’re working on (most array functions do this, unlike string functions), and assigning the output to the same variable will most likely cause a bug. You also forgot the () after shift. Then again, you could just assign the output of toUpperCase() directly to charArray[0].

for (x = 0; x < wordArray.length; x++) {
    
    var charArray = wordArray[x].split("");
    var firstLetter = charArray[0].toUpperCase();   
    charArray.shift();
    charArray.unshift(firstLetter);
    wordArray[x] = charArray.join("");
}
1 Like

Oh my goodness, thank you so much for helping me work through this!!!
So I need to put the toUpperCase() output somewhere, but I can’t assign it to charArray or I’ll ruin the array. So I’d have to assign it to charArray[0], and then I could do the join to whatever “x” of wordArray I"m on? I guess I just get confused about what you can do to manipulate arrays vs. what ends up just calling them.

Yeah, you have to be mindful of the types of data your variables are holding.