Basic algorithm scripting -> "Title Case a Sentence" Need Help please!

Basic algorithm scripting -> "Title Case a Sentence" Need Help please!
0

#1

Hey Community,

I have a lot of fun, solving the exercises.

I finaly found a solution for the “Title Case a Sentence”. It is not very elegant and way too long, but the code should work fine… But nonetheless it can not pass the tests. I tried it with every string, but it always returns the right data.

I could find another solution that works, but I really want to find out, what is wrong with the code, because I think understanding the bugs and solving them is a very important part of programming.

Any help is appreciated. Thank you in advance.
Ekko

var string = [];
function titleCase(str) {
  var strLow = str.toLowerCase();
  var words = strLow.split(" ");
  for (var word in words) {
    var x = words[word].split("");
    x[0] = x[0].toUpperCase();
    y = x.join("");
    x = [y];
    string.push(x);
  }
  str = string.join(" ");
  return str;
}
titleCase("I'm a little tea pot");

#2

Hey Ekko, nothing is wrong with your code, except one small detail: you defined string outside of the function. I mean it’s still works just like you said, but doesn’t pass the test)). But I tried to put it inside of function and it worked.
So just look into var string = [];
Good luck :+1:t2:


#3

Oh great, Thanks a lot! That really helped me.