Javascript字符串转换为'标题案例'不返回任何输出

时间:2016-04-18 20:31:31

标签: javascript arrays function

我无法确定代码中的错误,它不会产生任何输出:

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 个答案:

答案 0 :(得分:4)

我相信arr[x]应为arr[i]

&#13;
&#13;
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"));
&#13;
&#13;
&#13;

答案 1 :(得分:1)

简短的单行解决方案:

placeholderColor