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