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;
}