Get initials | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Get initials

I want to create a. Function that takes name as parameter and the function to return the initial of the name is uppercase Can I use Slice or CharAt method

10th Sep 2020, 9:13 PM
Tijan
Tijan - avatar
9 Answers
+ 5
Tijan You are welcome!
10th Sep 2020, 11:13 PM
Galaxy-Coding (inactive)
Galaxy-Coding (inactive) - avatar
+ 6
Use this: Note: name variable is your name. Initials will be logged in the console. You can change the code, of course var name = 'Foo Bar 1Name too Long'; var initials = name.match(/\b\w/g) || []; initials = ((initials.shift() || '') + (initials.pop() || '')).toUpperCase(); console.log(initials);
10th Sep 2020, 9:27 PM
Galaxy-Coding (inactive)
Galaxy-Coding (inactive) - avatar
+ 1
Galaxy-Coding (CodeWarrior) am still a newbie to javascript am finding it hard to understand😅
10th Sep 2020, 9:31 PM
Tijan
Tijan - avatar
+ 1
Galaxy-Coding (CodeWarrior) so i can write something like this function initials(name){ var match = name.match(/\b\w/g); match = ((match.shift() || ‘’) + (match.pop() || ‘’)).toUpperCase(); } console.log(initials(‘Hello World’));
10th Sep 2020, 9:44 PM
Tijan
Tijan - avatar
+ 1
would something like this work
10th Sep 2020, 9:44 PM
Tijan
Tijan - avatar
+ 1
Galaxy-Coding (CodeWarrior) thanks bro its worked ✅✅✅
10th Sep 2020, 10:19 PM
Tijan
Tijan - avatar
+ 1
cool cool
11th Sep 2020, 12:05 AM
Tijan
Tijan - avatar
+ 1
Anthony Maina thanks i will try it
11th Sep 2020, 4:39 PM
Tijan
Tijan - avatar
0
Tijan You can also try this: const initials= (name) => name.split(" ") .map (x => x[0].toUpperCase() ) .join("."); console.log(initials("John smith"));//prints J.S
11th Sep 2020, 4:37 PM
Anthony Maina
Anthony Maina - avatar