DEV Community

loading...

Discussion on: Daily Challenge #190 - capitalizeFirstLast

Collapse
navdeepsingh profile image
Navdeep Singh
console.clear();
const capitalizeFirstLast = (str) => {
  let strArr = str.split(" ");
  let parseStr = strArr.map(item => {
        itemArr = [...item];
        midStr = itemArr.slice(1,itemArr.length-1).join('');
        return item.length > 1 ? item[0].toUpperCase() + midStr.toLowerCase() + item[item.length-1].toUpperCase() : item[0].toUpperCase();      
  }).join(' ');
  console.log(parseStr);  
}
capitalizeFirstLast('my name is NAVDEEP singh')

JSBin Link