Title Case My Code

Tell us what’s happening:
here is my code its working properly but in output it gives undefined plus the main output

Your code so far

function titleCase(str) {
  var sum;
  var arr=str.toLowerCase().split(' ');
  for(var i=0;i<arr.length;i++)
    {for(var j=0;j<arr[i].length;j++)
      {
       if(j==0)    
         { 
       sum+=arr[i][0].toUpperCase();  
        if(j==arr[i].length-1)
        sum=sum+' '; 
         }
           else{
        sum+=arr[i][j];
        if(j==arr[i].length-1)
        sum=sum+' '; 
         
       }
      }
    }
  
 
    
  return sum;
}

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

Your browser information:

Your Browser User Agent is: Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36.

Link to the challenge:

Any way to correct it
i have tried sum=’’;but id doesn’t work but undefined is removed