简体   繁体   English

如何找到一个数组的总和,然后检查它的奇数还是偶数?

[英]How to find total sum of an array then checking if its odd or even in javascript?

I wrote the following code which works for cases with the array has more than one element. 我编写了以下代码,该代码适用于数组具有多个元素的情况。 How could I make it work when the array consist of just one element? 当数组仅包含一个元素时,如何使它起作用?

The below works for [1,23,4,5] but not for [0], [2], 1 以下内容适用于[1,23,4,5],但不适用于[0],[2], 1

 function oddOrEven(array) { var sum = array.reduce(function(acc, intialvalue) { return acc + intialvalue; }); if (sum % 2 == 0) { return "even" } else { return "odd" } } console.log(oddOrEven([1,23,4,5])) console.log(oddOrEven([0])) console.log(oddOrEven([1])) 

请检查此屏幕截图

As the error says, you must have an initial value, 0 in our case. 如错误所示,在我们的例子中,您必须具有一个初始值0。

 function oddOrEven(array) { var sum = array.reduce(function(acc, intialvalue) { return acc + intialvalue; }, 0); if (sum % 2 == 0) { return "even" } else { return "odd" } } console.log(oddOrEven([1,23,4,5])) console.log(oddOrEven([0])) console.log(oddOrEven([1])) 

No initial value means you need to set the initial value of the reduce funtion as 0. 没有初始值意味着您需要将reduce函数的初始值设置为0。

function oddOrEven(array) {
  var sum = array.reduce(function(acc, value) {
    return acc + intialValue;
  }, 0); // <- set the reduce functions initial value

  if (sum % 2 == 0) {
    return "even";
  } else {
    return "odd";
  }
}

You can clean this code up a little by re-writing it as 您可以将代码重写为

const oddOrEven = (array) => {
  const sum = array.reduce((acc, value) => acc + value, 0);

  return (sum % 2 == 0) ? "even" : "odd";
};

As others have pointed out, you will need to pass an initial value to reduce . 正如其他人指出的那样,您将需要传递一个初始值来reduce

As an alternate solution, you could just count whether there are are an even or number of odd elements in the array. 作为替代解决方案,您可以仅计算数组中是否存在偶数或奇数个元素。

 const oddOrEven = (array) => array.reduce((a, i) => i % 2 ^ a, 1) ? "even" : "odd"; console.log(oddOrEven([1,23,4,5])) console.log(oddOrEven([0])) console.log(oddOrEven([1])) 

Consider the following truth table: 考虑以下真值表:

x    y    | x+y
----------|-----
even even | even
even odd  | odd
odd  even | odd
odd  odd  | even

Give an initial value to your reduce call. 给您的reduce调用一个初始值。 You could also handle the case of an empty array, here I return undefinef: 您也可以处理空数组的情况,在这里我返回undefinef:

 const oddOrEven = arr => arr.length ? arr.reduce((sum, x) => sum + x, 0) % 2 === 0 ? 'even' : 'odd' : undefined; console.log(oddOrEven([1, 23, 4, 5])) console.log(oddOrEven([0])) console.log(oddOrEven([1])) console.log(oddOrEven([])) 

Bitwise & can be used to check even and odd 按位&可用于检查偶数和奇数

 function oddOrEven(array) { var sum = array.reduce((op, inp) => op + inp, 0); return sum & 1 ? 'odd' : 'even' } console.log(oddOrEven([1,23,4,5])) console.log(oddOrEven([0])) console.log(oddOrEven([1])) 

For the one-line lovers 对于单线恋人

 const oddOrEven = a => a.reduce((o,i) => o + i, 0) & 1 ? 'odd' : 'even' console.log(oddOrEven([1,23,4,5])) console.log(oddOrEven([0])) console.log(oddOrEven([1])) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM