Created
April 28, 2016 02:03
-
-
Save camark/8bb26846f68ebd8db021eef6e26af131 to your computer and use it in GitHub Desktop.
请把用户输入的不规范的英文名字,变为首字母大写,其他小写的规范名字。输入:['adam', 'LISA', 'barT'],输出:['Adam', 'Lisa', 'Bart']。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict' | |
function normalize(arr){ | |
function CapStr(s){ | |
var lc_s=s.split('').map(function(x){ | |
return x.toLowerCase() | |
}).reduce(function(x,y){ | |
return x.concat(y) | |
}) | |
return lc_s.charAt(0).toUpperCase()+lc_s.slice(1) | |
} | |
return arr.map(CapStr) | |
} | |
// 测试: | |
if (normalize(['adam', 'LISA', 'barT']).toString() === ['Adam', 'Lisa', 'Bart'].toString()) { | |
alert('测试通过!'); | |
} | |
else { | |
alert('测试失败!'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment