+ 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
9 Antworten
+ 5
Tijan
You are welcome!
+ 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);
+ 1
Galaxy-Coding (CodeWarrior) am still a newbie to javascript am finding it hard to understand😅
+ 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’));
+ 1
would something like this work
+ 1
Galaxy-Coding (CodeWarrior) thanks bro its worked ✅✅✅
+ 1
cool cool
+ 1
Anthony Maina thanks i will try it
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