0

I can't pinpoint where the error in the code is, it produces no output:

function capitalize (x) {
        var arr = x.split(' ');
        var output = [];

        for (var i = 0; i < arr.length; i++) {

            output.push(arr[x].charAt(0).toUpperCase()+arr[x].slice(1));
        };

        return output.join(' ');
    }

    alert(capitalize("the quick brown fox"));

2 Answers 2

4

I believe arr[x] should be arr[i].

function capitalize (x) {
        var arr = x.split(' ');
        var output = [];

        for (var i = 0; i < arr.length; i++) {

            output.push(arr[i].charAt(0).toUpperCase()+arr[i].slice(1));
        };

        return output.join(' ');
    }

    alert(capitalize("the quick brown fox"));

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, I was so used to using [x] as the placeholder haha.
1

Short one-line solution:

var str = "the quick brown fox",
    capitalized = str.split(" ").map(function(w){ return w[0].toUpperCase() + w.slice(1); }).join(" ");

console.log(capitalized);   // "The Quick Brown Fox"

1 Comment

That's awesome, I wouldn't of thought of the program being done like that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.