Basic Algorithm Scripting - Title Case a Sentence

Tell us what’s happening:
What does L => L. to upperCase stand for in this assignment? It confuses me since L is not present in every single word. So How does JS know what L stands for?

Your code so far

function titleCase(str) {
  return str
    .toLowerCase()
    .replace(/(^|\s)\S/g, L => L.toUpperCase());
}

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:107.0) Gecko/20100101 Firefox/107.0

Challenge: Basic Algorithm Scripting - Title Case a Sentence

Link to the challenge:

Hi there, where did you get this code? It uses arrow notation. Have you studied this yet?

We have blurred this solution so that users who have not completed this challenge can read the discussion in this thread without giving away the solution.

The code is from the solutions. This is the only one I understand, but where does the L come from? I understand about arrow notation, that one was feature in ES6. I just don’t understand what the ‘L’ means here. Is this similar to an [i] in for loops for example?

L is the name of the parameter

Ok, but how does JS know what L entails?

This is a feature of javascript called arrow functions.
It is equivalent to writing an anonymous function.
Not sure if you know any of these terms?

Edit: to revise these terms you can look at this lesson
https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/es6/write-arrow-functions-with-parameters

I understand how arrow functioning works, so basically ‘L’, since it is the first variable declared within the function, it automatically sees it as the first variable in, let’s say, an array?

I think I’m beginning to suspect the area of your confusion. Perhaps it is the -replace- function that is confusing you actually.

The way replace works here is it takes every letter in the str and it applies the pattern to it and if the pattern gives a match then it will pass that letter into the arrow function. The arrow function then produces an uppercase letter which is used to replace the original letter.

More about replace here
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace

How did you solve this? When you look at the solutions, its easier to understand them when you compare them with your solution.

I had around half of the code correct. I missed a few of the parameters present inside of the regex and I didn’t know exactly how to use the .toUppercase() this way. I just didn’t really understand what the L stood for in the solution.

You should still get your solution working first. That process of fixing a half right solution is a critical skill. We can help with that!

On the job, programmers are constantly using the skill of troubleshooting and fixing partially complete code.

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