Basic Algorithm Scripting: Title Case a Sentence**


Is there another way to do this easier.

function titleCase(str) {
let newArr=[];
let slicedArr=[];
let lowCaStr=str.toLowerCase();                 
let splitStr=lowCaStr.split(" ");

  //console.log(lowCaStr)  

      for (let i=0;i<splitStr.length;i++){          
            
           let firstLetter=splitStr[i].charAt(0);
      
           let capLetter=firstLetter.toUpperCase()
      
           newArr.push(capLetter);

            //console.log(capLetter)         
    }

  
        for (let i=0;i<splitStr.length;i++){          
            
           let slicedStr=splitStr[i].slice(1);         
             
          slicedArr.push(slicedStr);

          //console.log(slicedArr)    
    }            
    
let newArray = newArr.map((e, i) => e + slicedArr[i]);
newArray.toString();
//console.log(newArray.toString());
console.log(newArray.join(" "))
return newArray.join(" ")
}


titleCase("sHoRt AnD sToUt");

You can look at the Guide solutions to see other ways of completing this challenge.

1 Like