【问题标题】:A function that returns the number of even numbers of an array返回数组偶数个数的函数
【发布时间】:2021-10-18 17:49:03
【问题描述】:

我需要编写返回数组偶数个数的函数。

//======================  EXAMPLE  ========================
isEven([2,4,8,7])
3 // <======  EXPECTED OUTPUT
isEven([1,9,66,"banana"])
1 // <======  EXPECTED OUTPUT
//=========================================================

这是我写的:

function isEven(arr) {
        var count = 0;
        for (var i = 0; i < arr.length; i++) {
            if (arr[i] % 2 === 0) {
                count++;
                document.write(count);
            }
        }
    }

但对我来说它只是返回 undefined 我不知道为什么。

【问题讨论】:

  • 那是因为你不返回任何东西...... WTH
  • 它返回 undefined 因为你从不返回任何东西所以默认情况下 js 函数在这种情况下返回 undefined
  • 你在写文档,而不是返回
  • 你也应该避免document.write

标签: javascript arrays function


【解决方案1】:

实际上,人们永远不想将计算逻辑与任何类型的输出代码混合在一起。

此外,还可以使用Array.prototype.filter 和一个精确的过滤条件,它以odd 和/或even 数值为目标...类似于下一个提供的示例代码...

function getEvenCount(list) {
  return list.filter(item =>
    (typeof item === 'number') && (item % 2 === 0)
  ).length;
}
function getOddCount(list) {
  return list.filter(item =>
    (typeof item === 'number') && (item % 2 !== 0)
  ).length;
}

document.write(
  'getEvenCount([2,4,8,7]) ... ' +
  getEvenCount([2,4,8,7]) +
  '</br>'
)
document.write(
  'getEvenCount([1,9,66,"banana"]) ... ' +
  getEvenCount([1,9,66,"banana"]) +
  '</br>'
)
document.write(
  'getOddCount([2,4,8,7]) ... ' +
  getOddCount([2,4,8,7]) +
  '</br>'
)
document.write(
  'getOddCount([1,9,66,"banana"]) ... ' +
  getOddCount([1,9,66,"banana"]) +
  '</br>'
)

console.log(
  'getEvenCount([2,4,8,7]) ... ',
  getEvenCount([2,4,8,7])
);
console.log(
  'getEvenCount([1,9,66,"banana"]) ...',
  getEvenCount([1,9,66,"banana"])
);
console.log(
  'getOddCount([2,4,8,7]) ...',
  getOddCount([2,4,8,7])
);
console.log(
  'getOddCount([1,9,66,"banana"]) ...',
  getOddCount([1,9,66,"banana"])
);

【讨论】:

    【解决方案2】:

    因为函数没有return 任何东西。相反,它只是写入 HTML 文档(在循环的每次迭代中重复)。

    在函数结束时返回count

    function isEven(arr) {
        var count = 0;
        for (var i = 0; i < arr.length; i++) {
            if (arr[i] % 2 === 0) {
                count++;
                document.write(count);
            }
        }
        return count; // <--- here
    }
    

    顺便说一句,您应该彻底改掉使用document.write 的习惯。如果您想查看信息以进行调试,请使用console.log 将其记录到浏览器的开发控制台。如果您想在页面上查看输出,请查看如何识别和更新页面上 DOM 元素的文本/HTML。

    example section here 可能是一个不错的起点。基本上,您使用document.getElementByIddocument.querySelector 之类的东西来标识页面上的特定HTML 元素,然后将其innerHTMLinnerText 属性设置为要显示的数据。 (随着您继续学习和构建更多功能,它可能会涉及更多。)

    【讨论】:

    • 非常感谢!我会继续研究这些选择器。
    【解决方案3】:

    删除document.write,并在修复未定义问题的循环外返回计数。

    function isEven(arr) {
        var count = 0;
        for (var i = 0; i < arr.length; i++) {
            if (arr[i] % 2 === 0) {
                count++;
            }
        }
        return count;
    }
    

    alternatives to document.write,它允许浏览器实现在使用document.write时必须禁用的优化。

    使用console.log 调试到浏览器控制台。

    【讨论】:

    • 我会这样做的!我将继续学习 document.write 的替代方案。非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-03
    • 2021-02-10
    相关资源
    最近更新 更多