qappleh / Interview

我是追梦赤子心,公众号「深圳湾码农」的作者,某上市集团公司高级前端开发,深耕前端领域多年,每天攻破一道题,带你从0到1系统构建web全栈完整的知识体系!
https://github.com/qappleh/Interview
1.14k stars 95 forks source link

Day365:如何实现数组函数 reduce? #368

Open qappleh opened 3 years ago

qappleh commented 3 years ago

满足以下两个测试用例:

// => 55
reduce([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], (x, y) => x + y)
// => NaN
reduce([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], (x, y) => x + y, undefined)

TC39 规范在此: TC39 规范在此: https://tc39.es/ecma262/#sec-array.prototype.reduce (opens new window)。可参考标准,但无需按照标准实现。。可参考标准,但无需按照标准实现。