Discuss / JavaScript / 作业

作业

Topic source

第一题

function sum(...rest) {
 return arguments.length === 0?0: rest.reduce((x,y)=>{return x+y}) 
}

第二题

return (arguments.length===1?3.14:pi)*r*r;

为什么你这么优秀,给大佬顶个赞

我写错了,其实应该是这样,ES6了应该弃用arguments,直接用rest代替

function sum(...rest) {
 return rest.length === 0?0: rest.reduce((x,y)=>{return x+y}) 
}

第二个也是

return (rest.length===1?3.14:pi)*r*r;

篆愁君

#4 Created at ... [Delete] [Delete and Lock User]

reduce 高阶函数可传入第二个参数 ,0 以下写法即可

function sum(...rest) {

return reset.reduce((acc, cur) => acc + cur, 0);

}


  • 1

Reply