练习
请把用户输入的不规范的英文名字,变为首字母大写,其他小写的规范名字。输入:[‘adam’, ‘LISA’, ‘barT’],输出:[‘Adam’, ‘Lisa’, ‘Bart’]。
'use strict';
function normalize(arr) {
return arr.map(function(x){
var tt = [];
for(var i=0;i<x.length;i++){
if(i==0){
if (x[i]>='a' && x[i]<='z'){
tt.push(x[0].toUpperCase());
console.log(tt);
} else {
tt.push(x[0]);
console.log(tt);
}
} else {
if (x[i]>='A' && x[i]<='Z'){
tt.push(x[i].toLowerCase());
console.log(tt);
} else {
tt.push(x[i]);
console.log(tt);
}
}
}
console.log(tt);
return tt.reduce(function(x,y){
return x+y;
}
);
});
}
// 测试:
if (normalize(['adam', 'LISA', 'barT']).toString() === ['Adam', 'Lisa', 'Bart'].toString()) {
console.log('测试通过!');
}
else {
console.log('测试失败!');
}