实现说明
顶层就这几处状态:scores、best。
使用要点
- 每一步打印都带着自己的标签:
max of empty:、min of empty:、best:。
常见问题
运行前要先安装什么?
什么都不用额外装——运行时自带了代码所需的一切。 文件用顶层绑定保存自己的状态:scores、best。
核心语句
const best = scores.reduce((acc, n) => Math.max(acc, n), -Infinity);
代码示例
// Spread of an empty array gives Math.max/min no arguments at all
console.log('max of empty:', Math.max(...[]));
console.log('min of empty:', Math.min(...[]));
// With values, spread replaces the old apply(this, arr) idiom
const scores = [12, 98, 45];
console.log('best:', Math.max(...scores));
console.log('worst:', Math.min(...scores));
// A single NaN poisons the whole result
console.log('with NaN:', Math.max(...scores, NaN));
// -Infinity is the identity for max: safe seed when reducing by hand
const best = scores.reduce((acc, n) => Math.max(acc, n), -Infinity);
console.log('reduced best:', best);