资讯动态

JavaScript专项算法题(1):回调和高阶函数

发布时间:2026/10/21 1:18:41 来源:尧图企业网站定制
ORIGIN全文地址:JavaScript专项算法题(1):回调和高阶函数推荐:编程鼠标·电竞鼠标挑战1 addTwo问题:构建一个addTwo函数,作用为接受一个参数并将参数加2。题解:// Challenge 1functionaddTwo(num){returnnum+2;}// To check if you've completed it, uncomment these console.logs!console.log(addTwo(3));console.log(addTwo(10));挑战2 addS问题:构建一个addS函数,作用为接受一个参数并将参数与“S"拼接。题解:// Challenge 2functionaddS(word){returnword+"S";}// uncomment these to check your workconsole.log(addS('pizza'));console.log(addS('bagel'));挑战3 map问题:构建一个map函数,其接受两个参数:数值型数组回调函数——一个应用于上述数值型数组中的每一个元素的函数(于map函数内)map函数的返回值为包含上述数值型数组元素逐个运行回调函数后生成的元素值的新数组。map([1,2,3,4,5],multiplyByTwo);//- [2,4,6,8,10]multiplyByTwo(1);//- 2multiplyByTwo(2);//- 4题解:// Challenge 3functionmap(array,callback){varnewArray=[];for(leti=0;iarray.length;i++){newArray.push(callback(array[i]));}returnnewArray;}console.log(map([1,2,3],addTwo));挑战4forEach问题:函数forEach接受一个数组和一个回调函数,运行回调函数于输入数组的每一个元素。forEach函数无返回值。letalphabet='';constletters=['a','b','c','d'];forEach(letters,function(char){alphabet+=char;});console.log(alphabet);//prints 'abcd'题解:// Challenge 4functionforEach(array,callback){for(leti=0;iarray.length;i++){callback(array[i]);}}// see for yourself if your forEach works!letalphabet='';constletters=['a','b','c','d'];forEach(letters,function(char){alphabet+=char;});console.log(alphabet);//prints 'abcd'挑战5 mapWith问题:在这个挑战中,你需要将map函数重构为mapWith。这一次你要在mapWith中使用forEach函数而不是使用for循环。题解:// Challenge 5functionmapWith(array,callback){varnewArray=[];forEach(array,function(item){newArray.push(callback(item))});returnnewArray;}console.log(mapWith([1,2,3],addTwo));挑战6 reduce问题:函数reduce接受一个数组并将数组内的所有值合并为一个值。比如,它可以将数组求和,求积,以及其它你想加进函数中的操作。constnums=[4,1,3];constadd=function(a,b){returna+b;}reduce(nums,add,0);//- 8以下是它的运行原理。函数有一个“累加器值”(第三个参数),作用为充当初始值并且累加每一次循环的输出值。数组参数会被遍历,传递“累加器值“和新的数组元素值作为参数到回调函数中。回调函数的返回值会成为新的”累加器值“。下一个循环会使用这个新”累加器值“。在上面的例子中,”累加器值“刚开始为0,调用add(0, 4),”累加器值“变为4,然后add(4, 1)将其变为5,最后add(5, 3)得到8并最终返回。题解:// Challenge 6functionreduce(array,callback,initialValue){// // Solution 1:// var callbackValue = 0;// var finalResult = 0;// for(let i = 0; iarray.length; i++) {// if (i == 0){// callbackValue = array[i];// finalResult = callback(callbackValue, initialValue);// } else {// finalResult = callback(finalResult, array[i]);// }// }// return finalResult;// Solution 2:letreduceValue=initialValue;for(leti=0;iarray.length;i++){reduceValue=callback(array[i],reduceValue);}returnreduceValue;}constnums=[4,1,3];constadd=function(a,b){returna+b;}console.log(reduce(nums,add,0));//- 8挑战7 intersection问题:构建intersection函数,作用为比较输入进来的多组数组并返回一个包含数组间共同元素的新数组。奖励:使用reduce!题解:// Challenge 7functionintersection(arrays){// Solution 1 (without reduce):// var obj = {};// for(let i=0; iarrays.length; i++){// if(i==0){// for(var j=0;jarrays[i].length; j++){// obj[arrays[i][j]]=arrays[i][j];// }// } else {// for(var j=0;jarrays[i].length; j++){// // console.log(obj[arrays[i][j]])// if(obj[arrays[i][j]] != undefined){// obj[arrays[i][j]]= obj[arrays[i][j]] + 1;

读完文章,也想定制专属网站?

尧图设计师 24 小时内与您沟通定制方案

免费获取报价 →
↑