Not the best solution: ))
function findMiddleName(str) { const words = str.split(' '); let result = ''; if(words.length <= 2) return result = str else{ let middleNames = '' for(let i = 1; i < (words.length - 1); i++) middleNames += (words[i].slice(0,1) + '. ') return result = `${str.split(' ')[0]} ${middleNames}${str.split(' ')[words.length - 1]}` } } console.log(findMiddleName('Jack Ryan')); console.log(findMiddleName('Lois Mary Lane')); console.log(findMiddleName('Dimitri')); console.log(findMiddleName('Alice Betty Catherine Davis'));
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Not the best solution: ))