Title Case a Sentence error! Is it a bug or something else?

    I have done the challenge in this way
function titleCase(str) {
  var arr=str.split(" ");
  var newstr="",word="";
  for(var i=0;i<arr.length;i++){
    var temparr=arr[i].split("");
      newstr+=" "+word;
  return newstr;

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

I am gating the right output in console but the challenge is not completing and showing an error!:cry: can someone help me where i am getting wrong. Thanks in advance.

The length of your string after doing a titleCase() is 1 more than the original string length.
You have a leading space in front of your output string. That’s causing the test to fail.
you don’t need the variable newstr and word.
also, instead of doing a toLowerCase() on each character of word, just apply it to the whole sentence in the beginning.

function titleCase(str) {
  var arr = str.toLowerCase().split(' ');
  var temparr = [];
  for(var i=0; i<arr.length; i++){
    temparr = arr[i].split('');
    arr[i] = temparr.join('');
  return arr.join(' ');
titleCase("I'm a little tea pot");
1 Like

Thanks for your quick reply!! its really works for me:grin: Thanks for expressing your concern!

I cleaned up your code.
You need to use triple backticks to post code to the forum.
See this post for details.