I need to understand this part of the code

let x = "hello my name is chrono" const finalSentence = x.replace(/\S\w | \s\w/g, letter => letter.toUpperCase()); >>>>>>>>>>>>> I need to understand this part of the code >>>>>>>>>>>> letter => letter.toUpperCase() Is it like writing this function letter(letter) { letter.toUpperCase() return letter }
12 Replies
13eck
13eck2y ago
Exactly, yes It's called an arrow function. Give me a sec to find the MDN link
13eck
13eck2y ago
Arrow function expressions - JavaScript | MDN
An arrow function expression is a compact alternative to a traditional function expression, but is limited and can't be used in all situations.
chrono1913
chrono19132y ago
but I want to know for that part of the code, could you write an outside function and pass it into that part of the code like const finalSentence = x.replace(/(^\w{1})|(\s+\w{1})/g, letter()); function letter(letter) { letter.toUpperCase() return letter }
Joao
Joao2y ago
Yes, you can do that as well But, instead of invoking the function like in your question, you just provide the reference to that function:
// before
const finalSentence = x.replace(/\S\w | \s\w/g, letter());

// after
const finalSentence = x.replace(/\S\w | \s\w/g, letter);
// before
const finalSentence = x.replace(/\S\w | \s\w/g, letter());

// after
const finalSentence = x.replace(/\S\w | \s\w/g, letter);
chrono1913
chrono19132y ago
but its still giving me an error did I write the function correctly
Joao
Joao2y ago
and the error is? always read the errors, we can't really do anything without this information
chrono1913
chrono19132y ago
o forgot the closing bracket but not the output is “helundefinedundefinednaundefinedundefinedchrono”
Joao
Joao2y ago
And what do you think may cause that What is the expected output vs the actual output?
chrono1913
chrono19132y ago
i think its the function there isn’t any parameter in the function right> ? "Hello My Name Is Chrono"
Joao
Joao2y ago
The issue is not the function, it's the pattern. Read the description on how replace works when you provide a function and you will see the problem: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace#specifying_a_function_as_the_replacement Hint: in the output you are seeing a bunch of "undefined" in between the text
chrono1913
chrono19132y ago
when you say pattern do you mean the REGEXP /(^\w{1})|(\s+\w{1})/g thats what I was using
MarkBoots
MarkBoots2y ago
const input = "hello my name is chrono";
const output = input.replace(/\b\w/g,(m)=>m.toUpperCase());
console.log(output) // "Hello My Name Is Chrono"
const input = "hello my name is chrono";
const output = input.replace(/\b\w/g,(m)=>m.toUpperCase());
console.log(output) // "Hello My Name Is Chrono"
\b word boundary \w any word character /g global, all matched (m)=> ... for each match So every first word character after a word boundary will be matched and replaced or if you want the replacement outside the replace function
const input = "hello my name is chrono";
const output = input.replace(/\b\w/g, replaceFunction);
console.log(output) // "Hello My Name Is Chrono"

function replaceFunction(match){
return match.toUpperCase()
}
const input = "hello my name is chrono";
const output = input.replace(/\b\w/g, replaceFunction);
console.log(output) // "Hello My Name Is Chrono"

function replaceFunction(match){
return match.toUpperCase()
}
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace
String.prototype.replace() - JavaScript | MDN
The replace() method returns a new string with one, some, or all matches of a pattern replaced by a replacement. The pattern can be a string or a RegExp, and the replacement can be a string or a function called for each match. If pattern is a string, only the first occurrence will be replaced. The original string is left unchanged.