使用 JavaScript 中的递归查找数组的乘积

javascriptweb developmentfront end technologyobject oriented programming更新于 2024/1/9 8:12:17

我们需要编写一个 JavaScript 函数,该函数接受一个整数数组。我们的函数应该做以下两件事 −

  • 使用递归方法。

  • 计算数组中所有元素的乘积。

最后,它应该返回乘积。

例如 −

如果输入数组是 −

const arr = [1, 3, 6, .2, 2, 5];

那么输出应该是 −

const output = 36;

示例

其代码为 −

const arr = [1, 3, 6, .2, 2, 5];
const arrayProduct = ([front, ...end]) => {
   if (front === undefined) {
      return 1;
   };
   return front * arrayProduct(end);
};
console.log(arrayProduct(arr));

输出

控制台中的输出将是 −

36

相关文章


有用资源