Reputation: 57
I think I am close but I would to get your feedback to solve this using a derivative of the code I have already created, I pass the following tests, but I am struggling to pass the final test as I need to return two middle names abbreviated, and at the moment I can only return the first. The tests below show the function and parameters passed, and the expected result its the last ione I am struggling with. I would appreciate your expert advice. Kind regards, Jon
Test.assertEquals(initializeNames('Jack Ryan'), 'Jack Ryan', '');
Test.assertEquals(initializeNames('Lois Mary Lane'), 'Lois M. Lane', '');
Test.assertEquals(initializeNames('Dimitri'), 'Dimitri', '');
Test.assertEquals(initializeNames('Alice Betty Catherine Davis'), 'Alice B. C. Davis', '')
function initializeNames(name) {
let seperateNames = name.split(' ');
let output = "";
if (name.length = 2) {
output = name;
}
for (let i = 1; i < seperateNames.length - 1; i++) {
output = seperateNames[i];
let abvName = output.substring(0, 1) + '.';
output = seperateNames[0] + ' ' + abvName + ' ' + seperateNames.slice(-1);
}
return output;
}
Upvotes: 3
Views: 68
Reputation: 6501
Maybe I understood your question incompletely, however if your only concern is getting the initials of the middle words, you might do:
function initNames(str){
return str.trim()
.replace(
/\s(?=\b)(\w+)(?=\b\s)/gi,
(m,g)=> " " + g.slice(0,1) + "."
)
}
console.log(initNames(' Alice Betty Catherine Davis '));
console.log(initNames(' Alice Betty Davis '));
console.log(initNames(' Alice Davis '));
Upvotes: 0
Reputation: 3714
Pass Any number of parameters. Will always abbreviate all words other than first and last word.
console.log(initializeNames('Jack Ryan'));
console.log(initializeNames('Lois Mary Lane'));
console.log(initializeNames('Dimitri'));
console.log(initializeNames('Alice Betty Catherine Davis'));
console.log(initializeNames('Alice Betty Ketty Catherine Davis'));
console.log(initializeNames('Alice Ketty Betty Metty Catherine Davis'));
console.log(initializeNames('Alice Ketty David Mical Catherine Davis, Louis'));
function initializeNames(name) {
let seperateNames = name.split(' ');
let output = name;
if(seperateNames.length>2){
const firstName = seperateNames[0];
let middleName = "";
for(let i=1; i<=seperateNames.length-2; i++){
middleName += " "+seperateNames[i][0]+".";
}
const lastName = seperateNames[seperateNames.length-1];
output = firstName + middleName +" "+ lastName;
}
return output;
}
Upvotes: 0
Reputation: 5623
Here is a function which perform the initialization of the given name and check the length of the array obtain after splitting the name, and based on the result it perform the update of the array value based on their index.
let names = ['Jack Ryan',
'Lois Mary Lane',
'Dimitri',
'Alice Betty Catherine Davis',
'John Alexander Davis Smith Doe'
];
function initializeNames(name) {
let words_lists = name.split(' ');
return words_lists.length === 1? name: words_lists.reduce((accumulator, current, index) => {
if(index === 0 || index === words_lists.length -1) {
return accumulator.concat(current);
} else {
return accumulator.concat(`${current.charAt(0)}.`);
}
}, []).join(' ');
}
names.forEach(name => console.log(initializeNames(name)));
Based on the length of the name provide it performs the update as only the first and the last part of the name remain unchanged.
Upvotes: 0
Reputation: 193258
Split the string, use array spread and rest to get the first word, and the rest of the words, and the use slice to get the middle and the last words. Combine them all to a single array, map the middle words to the required form, and then flatten and join with spaces:
const initializeNames = str => {
const [first, ...rest] = str.split(' ')
const middle = rest.slice(0, -1)
const last = rest.slice(-1)
return [
first,
middle.map(([c]) => `${c.toUpperCase()}.`),
last
].flat().join(' ')
}
const assertEquals = (a, b) => console.log(`"${a}" & "${b}" are ${a === b ? 'equal' : 'not equal'}`)
assertEquals(initializeNames('Jack Ryan'), 'Jack Ryan');
assertEquals(initializeNames('Lois Mary Lane'), 'Lois M. Lane');
assertEquals(initializeNames('Dimitri'), 'Dimitri', '');
assertEquals(initializeNames('Alice Betty Catherine Davis'), 'Alice B. C. Davis')
Upvotes: 0
Reputation: 60
function initializeNames(name) {
const [first, ...rest] = name.split(' ');
return !rest
? first
: rest.length === 1
? name
: rest.length === 2
? `${first} ${rest[0][0]}. ${rest[1]}`
: `${first} ${rest[0][0]}. ${rest[1][0]}. ${rest[2]}`;
}
Any other cases of the rest
?
Upvotes: 0
Reputation: 20701
This if condition is useless. Because =
is assignment and ==
is equality check.
if (name.length = 2) {
output = name;
}
Also I believe you wanted to check the length of seperateNames
array. You already know what to do if length is less than equal to 2.
But if it is not then you need your loop and extra calculation.
Have a look at below code which starts with
the first name, and
add the abvName
, and then
the last name
console.log(initializeNames('Jack Ryan'));
console.log(initializeNames('Lois Mary Lane'));
console.log(initializeNames('Dimitri'));
console.log(initializeNames('Alice Betty Catherine Davis'));
function initializeNames(name) {
let seperateNames = name.split(' ');
let output = "";
if (seperateNames.length <= 2) {
output = name;
}
else{ output = seperateNames[0];
for (let i = 1; i < seperateNames.length - 1; i++) {
let currentOutput = seperateNames[i];
let abvName = currentOutput.substring(0, 1) + '.';
output = output + ' ' + abvName + ' ';
}
output += seperateNames[seperateNames.length - 1];
}
return output;
}
Note : x += b;
is the same as x = x + b;
Upvotes: 1