Description
- Write a function to convert a name into initials.
- This kata strictly takes two words with one space in between them.
Solution 01
function abbreviateName(name) {
let arr = name.split(' ');
let result = [];
for (let i = 0; i < arr.length; i++) {
result.push(arr[i][0].toUpperCase());
}
return result.join('.');
}
abbreviateName('Sam Azor'); // S.A
abbreviateName('Ralph Donovan'); // R.D
abbreviateName('Becky Daniel'); // B.D
abbreviateName('Lauren Hernandez'); // L.H
Solution 02
function abbreviateName(name) {
let arr = name.split(' ');
return (arr[0][0] + '.' + arr[1][0]).toUpperCase();
}
abbreviateName('Sam Azor'); // S.A
abbreviateName('Ralph Donovan'); // R.D
abbreviateName('Becky Daniel'); // B.D
abbreviateName('Lauren Hernandez'); // L.H
Solution 03
function abbreviateName(name) {
let arr = name.toUpperCase().split(' ');
return arr[0][0] + '.' + arr[1][0];
}
abbreviateName('Sam Azor'); // S.A
abbreviateName('Ralph Donovan'); // R.D
abbreviateName('Becky Daniel'); // B.D
abbreviateName('Lauren Hernandez'); // L.H
Solution 04
function abbreviateName(name) {
return name.split(' ').map(i => i[0].toUpperCase()).join('.');
}
abbreviateName('Sam Azor'); // S.A
abbreviateName('Ralph Donovan'); // R.D
abbreviateName('Becky Daniel'); // B.D
abbreviateName('Lauren Hernandez'); // L.H
Solution 05
function abbreviateName(name) {
return name.split(' ').map(i => i.substr(0, 1).toUpperCase()).join('.');
}
abbreviateName('Sam Azor'); // S.A
abbreviateName('Ralph Donovan'); // R.D
abbreviateName('Becky Daniel'); // B.D
abbreviateName('Lauren Hernandez'); // L.H
Solution 06
function abbreviateName(name) {
return name.match(/\b\w/g).join('.').toUpperCase();
}
abbreviateName('Sam Azor'); // S.A
abbreviateName('Ralph Donovan'); // R.D
abbreviateName('Becky Daniel'); // B.D
abbreviateName('Lauren Hernandez'); // L.H