//高阶函数:一个函数可以接受另一个函数作为参数,这种函数称之为高阶函数。
/**/
function f(x,y,f){
return f(x)+f(y);
}
var sumAbs=f(-6,4,Math.abs);
console.log(sumAbs);//
//map和reduce
/*map*/
/**/
function pow(x){
return x*x;
}
var arr=[1,2,3,4,5];
var x2=arr.map(pow);
var toS=arr.map(String);
console.log(x2);//[1, 4, 9, 16, 25]
console.log(toS);//["1", "2", "3", "4", "5"]
/*reduce*/
/**/
var arr=[1,3,5,7,9];
function sum(x,y){
return x+y;
}
function pro(x,y){
return x*y;
}
function intt(x,y){
return x*10+y;
}
var s=arr.reduce(sum);
var p=arr.reduce(pro);
var i=arr.reduce(intt);
console.log(s);//
console.log(p);//
console.log(i);//
/*or*/
console.log(arr.reduce(function(x,y){
return x+y;
}));//
console.log(arr.reduce(function(x,y){
return x*y;
}));//
console.log(arr.reduce(function(x,y){
return x*10+y;
}));//
/*string to int*/
/**/
var str="5678";
var arr=str.split('');
console.log(arr);//["5", "6", "7", "8"]
arr=arr.map(function(x){
return x-0;
})
console.log(arr);//[5, 6, 7, 8]
console.log(arr.reduce(function(x,y){
return x*10+y;//
}))
/*or*/
console.log(str.split('').map(function(x){
return x-0;
}).reduce(function(x,y){
return x*10+y;
}));//
/*请把用户输入的不规范的英文名字,变为首字母大写,其他小写的规范名字。输入:['adam', 'LISA', 'barT'],输出:['Adam', 'Lisa', 'Bart']。*/
/*test*/
var str="abs";
console.log(str[0]);//a
/**/
var arr=['adam', 'LISA', 'barT'];
console.log(arr.map(function(x){
return x[0].toUpperCase()+x.substring(1).toLowerCase()
}));// ["Adam", "Lisa", "Bart"]