给定一个数字列表,请确定其元素的总和是奇数还是偶数。
将答案作为字符串匹配"odd"或"even"。如果输入数组为空,则将其视为:([0]具有零的数组)
function oddOrEven(array) {
var sum = 0;
for(var i = 0;i
sum = sum+array[i];
}
if(sum%2==0){
return "even";
}else{
return "odd";
}
}
测试:
console.log(oddOrEven([0]))//'even'
console.log(oddOrEven([1]))// 'odd'
console.log(oddOrEven([]))// 'even'
console.log(oddOrEven([0,1,5]))// 'even'
console.log(oddOrEven([0, -1, 2]))// 'odd'
console.log(oddOrEven([-1023, 1, -2]))// 'even'