DEV Community

loading...

Discussion on: Daily Challenge #38 - Middle Name

Collapse
devendradhanal profile image
Devendra Dhanal
const initializeMiddleNames = (name) => {
    const nameArr = name.split(' ')
    for (let index = 1; index < nameArr.length -1; index++) {
        nameArr[index] = `${nameArr[index].substring(0, 1)}.`;
    }
    return nameArr.join(' ') 
}


initializeMiddleNames('Jack Ryan') // Jack Ryan
initializeMiddleNames('Lois Mary Lane') // Lois M. Lane
initializeMiddleNames('Dimitri') // Dimitri
initializeMiddleNames('Alice Betty Catherine Davis') // Alice B. C. Davis