-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathabbreviate-a-two-word-name.js
80 lines (70 loc) · 2.66 KB
/
abbreviate-a-two-word-name.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
function abbrevName(name) {
// a place to store the first initial - initialized to the first letter in the name
const firstInitial = name[0];
// a place to store the second initial - initialized to the empty string
let secondInitial = '';
// iterate over the characters in the string - starting at the 2nd character
for (let i = 1; i < name.length; i++) {
const letter = name[i];
// if the current character is a space
if (letter === ' ') {
// set the second initial to the next character
secondInitial = name[i + 1];
break;
}
}
// return the first initial concatenated with a period concatenated with the second initial
return firstInitial.toUpperCase() + '.' + secondInitial.toUpperCase();
}
function abbrevName(name) {
const firstNameLastName = name.split(' ');
const firstName = firstNameLastName[0];
const lastName = firstNameLastName[1];
const firstInitial = firstName[0];
const lastInitial = lastName[0];
return firstInitial.toUpperCase() + '.' + lastInitial.toUpperCase();
}
function abbrevName(name) {
const firstNameLastName = name.split(' ');
return firstNameLastName[0][0].toUpperCase() + '.' + firstNameLastName[1][0].toUpperCase();
}
function abbrevName(name) {
return name.split(' ')[0][0].toUpperCase() + '.' + name.split(' ')[1][0].toUpperCase();
}
function abbrevName(name) {
const [firstName, lastName] = name.split(' ');
const firstInitial = firstName[0];
const lastInitial = lastName[0];
return firstInitial.toUpperCase() + '.' + lastInitial.toUpperCase();
}
function abbrevName(name) {
const [{
0: firstInitial
}, {
0: lastInitial
}] = name.split(' ');
return firstInitial.toUpperCase() + '.' + lastInitial.toUpperCase();
}
function abbrevName(name) {
const [[firstInitial], [lastInitial]] = name.split(' ');
return firstInitial.toUpperCase() + '.' + lastInitial.toUpperCase();
}
function abbrevName(name) {
const firstNameLastName = name.split(' ');
const firstName = firstNameLastName[0];
const lastName = firstNameLastName[1];
const firstInitial = firstName[0];
const lastInitial = lastName[0];
// return firstInitial.toUpperCase() + '.' + lastInitial.toUpperCase();
// return `${firstInitial}.${lastInitial}`.toUpperCase();
return (firstInitial + '.' + lastInitial).toUpperCase();
}
function abbrevName(name) {
return name.replace(/([a-z])[a-z]* ([a-z])[a-z]*/i, '$1.$2').toUpperCase();
}
console.log(abbrevName('Sam Harris'), 'S.H');
console.log(abbrevName('Patrick Feenan'), 'P.F');
console.log(abbrevName('Evan Cole'), 'E.C');
console.log(abbrevName('P Favuzzi'), 'P.F');
console.log(abbrevName('David Mendieta'), 'D.M');
console.log(abbrevName('david mendieta'), 'D.M');