【发布时间】:2015-11-10 17:59:19
【问题描述】:
var name = "AlbERt EINstEiN";
function nameChanger(oldName) {
var finalName = oldName;
// Your code goes here!
finalName = oldName.toLowerCase();
finalName = finalName.replace(finalName.charAt(0), finalName.charAt(0).toUpperCase());
for(i = 0; i < finalName.length; i++) {
if (finalName.charAt(i) === " ")
finalName.replace(finalName.charAt(i+1), finalName.charAt(i+1).toUpperCase());
}
// Don't delete this line!
return finalName;
};
// Did your code work? The line below will tell you!
console.log(nameChanger(name));
我的代码原样返回“阿尔伯特·爱因斯坦”。我想知道我哪里出错了? 如果我添加
console.log(finalName.charAt(i+1));
在 if 语句之后,并注释掉其余部分,它会打印“e”,因此它会像它应该的那样识别 charAt(i+1)...我只是无法让它将第二个单词的第一个字母大写。
【问题讨论】:
标签: javascript string