Pages

Sunday, February 28, 2016

TITLE CASE A SENTENCE

Today's challenge was to return the provided string with the first letter of each word capitalized. The rest of the words are to be in lower case.

For e.g     If input is following string.

                "I'm a little tea pot"

The output should be

                 "I'm A Little Tea Pot"

I  finally succeeded in implementing the solution. Although I was tempted to look for solution in google, but I kept on revising by solutions based on output at every step using "console.log()". Finally my solution was accepted. I know at lot can be improved in my solution. but this solution is my solution and is not a copy paste from google.

Here is my solution:-
 
  function titleCase(str) {
  
   var arr = str.toLowerCase().split(' ');
   arr1 = [];
   
   for (var i = 0; i < arr.length; i++){
           var newArr = arr[i].split('');
           
           var j = newArr[0].toUpperCase();


           newArr.shift();
           
           var k = newArr.unshift(j);
           
           arr1.push(newArr.join(''));          
           
   }
  return arr1.join(' ');
}

titleCase("I'm a little tea pot");

No comments:

Post a Comment