数字数组的均等分割-JavaScript
如果我们可以将一个数组分为一个元素和其余元素,则我们需要编写一个返回true的函数,以使该一个元素等于除自身之外的所有其他元素的乘积,否则返回false。
例如:如果数组是-
const arr = [1, 56, 2, 4, 7];
那么输出应该为真
因为56等于-
2 * 4 * 7 * 1
示例
以下是代码-
const arr = [1, 56, 2, 4, 7];
const isEqualPartition = arr => {
const creds = arr.reduce((acc, val) => {
let { prod, max } = acc;
if(val > max || !max){
prod *= (max || 1);
max = val;
}else{
prod *= val;
}
return { prod, max };
}, {
prod: 1,
max: null
});
return creds.max === creds.prod;
};
console.log(isEqualPartition(arr));输出结果
以下是控制台中的输出-
true
热门推荐
1 狗狗下楼训练
3 狗狗训练头盔
6 狗狗训练机构
7 训练狗狗步骤
9 狗狗能训练
10 狗狗基本动作训练方法
11 怎么训练狗狗叼饭盆
12 狗狗一岁了还能训练吗
13 训练狗狗的
14 宁波狗狗训练
15 训练狗狗拉屎
16 教狗狗训练
17 狗狗要怎么训练上厕所
18 狗狗靠的训练