Question about "Title Case a Sentence"

My question is specifically about Solution 2.

function titleCase(str) {
  var convertToArray = str.toLowerCase().split(" ");
  var result = convertToArray.map(function(val) {
    return val.replace(val.charAt(0), val.charAt(0).toUpperCase());
  });
  return result.join(" ");
}

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

I was going for a similar approach to Solution 2, but I finally gave in. I was getting close but when I saw the solution, I was never going to get it right. Anyways, what I specifically don’t understand is where does “val” come from? is that a paremeter built-in to JS? I just see it doesn’t get defined at all yet the code runs. If it is a built-in parameter (like say, “prop” and “id” which also sent me spinning), where can I find a list of these built-in parameters? It’s driving me nuts seeing these (what appear to me) random undefined variables all over the code.
Any info appreciated. Thank you.

Copying the solutions will make it harder and harder to keep up with the challenges and understand what’s going on. I recommend instead asking questions on the forum to help you get un-stuck.


Anyways, what do you know about the map() method? val is the argument to the callback function for the map() method. It is not any sort of special variable name.

Similarly, prop and id are not random or undefined or special ‘built in’ parameters either. They are just some names of some variables.


const myArr = [1, 2, 3, 4];
const myDoubledArray = myArr.map(function(someVariableName) {return 2 * someVariableName});

Any variable name can be used. val is not special. It’s just a variable name.

1 Like

I understand. val is a callback function. I mistook it for a variable.
Sometimes I’d rather ask stupid questions than waste time overthinking things.
Thank you.

val is not a callback function. It is a variable inside of a callback function. I think you may have some misunderstandings on how function arguments work.

Thank you for pointing that out, Jeremy.
I will work on it.

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.