My working 'Title Case A Sentence' algorithm won't pass!

Hi people,

First post here but I’d love to get feedback on why this specific code won’t pass. I don’t want to just check working solutions, I want to learn what I’ve done wrong.

var titleCaseArray = [];

    function titleCase(str) {
        var myArray = str.split(' ');
        for (var i = 0; i <myArray.length; i++) {
        str = titleCaseArray.join(" ");
        return str;


titleCase(“sHoRt AnD sToUt”);

I was really keen to make this work without cheating or looking up too many clues, so I just though about it, broke it down into what I wanted to do, then put it together as best I could. I’m guessing my code is a bit eccentric, and there’s probably a much cleaner way, but hey it works perfectly! Why won’t it pass the test?

I’m actually quite proud of myself for making this work. I’ve been finding coding hard and this is one of the first challenges I’ve done without looking too much up or thinking about it for too long. Felt like I was making a breakthrough, so it’s maddening to now be stuck…

Any advice much appreciated.

Don’t use global variables - declare titleCaseArray local inside the function

Use console.log liberally to see what is happening in your code

I think ppc has it right about the global variable.

Something else to note is that while this will work, you might refactor it to make it clearer and cleaner. For instance, you could use toLowerCase just once on the initial string instead of in the for loop. You could also use map instead of a for loop and push.

Thanks guys.

I moved the first variable inside the function and it worked!

The challenge must be set up to process just the function block.

Onto the next one :slight_smile: