Javascript减少功能后无法正常工作?

时间:2018-03-22 14:47:00

标签: javascript arrays function reduce

不太确定我在哪里出错了,期望通过将5转换为[1,2,3,4,5]的字符串并将5(1 * 2 * 3 * 4 * 5 = 120)因子化然后使用reduce将所有字符串相乘。当我运行代码时,它只给了我[1,2,3,4,5] ......

 var arr = [];

function factorialize(num) {

 for (var i = 1; i <= num; i++) {
  arr.push(i);
}
  return arr;
}

var factors = 0;

factors = arr.reduce(function(previousVal, currentVal) {
  return previousVal * currentVal;
}, 0); // Expecting 120, instead result = [1,2,3,4,5]

factorialize(5);

原谅漫长的路线 - 我第一周的Javascript!

6 个答案:

答案 0 :(得分:2)

arr为空,你应该首先给它得到因子化的结果数组,你应该乘以,而不是加,当乘以时,起始值是1而不是0:

&#13;
&#13;
var arr = [];

function factorialize(num) {
  for (var i = 1; i <= num; i++) {
    arr.push(i);
  }
  return arr;
}

arr = factorialize(5); // give it the value

var factors = arr.reduce(function(previousVal, currentVal) {
  return previousVal * currentVal; // multiply, don't add
}, 1); // start with 1 when multiplying

console.log(arr);
console.log(factors);
&#13;
&#13;
&#13;

如果您只想计算阶乘:

&#13;
&#13;
function factorial(num) {
  var res = 1;
  for (var i = 2; i <= num; i++) {
    res *= i;
  }
  return res;
}
console.log('factorial(5) = ' + factorial(5));
console.log('factorial(10) = ' + factorial(10));
&#13;
&#13;
&#13;

答案 1 :(得分:1)

您可以先获得因子,然后乘以Array#reduce因子。

我建议将函数命名为它的作用并在函数内部移动数组声明,因为函数返回此数组。

要获取产品,您需要将这些值相乘,并使用1作为中性起始值,以便从数字中获取产品。

function getFactors(num) {
    var i, arr = [];

    for (i = 1; i <= num; i++) {
        arr.push(i);
    }

    return arr;
}

var factors = getFactors(5),
    product = factors.reduce(function(previousVal, currentVal) {
        return previousVal * currentVal;
    }, 1);

console.log(factors);
console.log(product);

答案 2 :(得分:1)

var arr = [];
function factorialize(num) {
    for (var i = 1; i <= num; i++) {
        arr.push(i);
    }
    var factors = 0;
    factors = arr.reduce(function (previousVal, currentVal) {
        return previousVal * currentVal;
    });
    return factors
}

factorialize(5); // 120

答案 3 :(得分:1)

您没有致电factorsfactorialize(5);通过执行此操作,您只需调用function factorialize(num),它将为您提供数组(1 ... num)。

(附加信息)并且在减少时,您正在添加+乘法*的实例,因此也要更改

factors = arr.reduce(function(previousVal, currentVal) {
  return previousVal + currentVal;
}, 0);
   ^
   |_  either initialize it to 1 or remove this.

见下面的代码。我只是创建数组然后对该数组应用reduce。

function factorialize(num) {
 var arr = [];
 for (var i = 1; i <= num; i++) {
  arr.push(i);
 }
 return arr.reduce(function(previousVal, currentVal) {
      return previousVal * currentVal;
  });
}


console.log(factorialize(5));

答案 4 :(得分:0)

  • 将全局变量arr放在函数factorialize
  • 获取返回的数组,然后执行函数reduce
  • 您需要相乘而不是添加数字。
  • 使用initialValue = 1启动reduce,这是为了避免0 * n

function factorialize(num) {
  var arr = [];
  for (var i = 1; i <= num; i++) {
    arr.push(i);
  }
  return arr;
}

var arr = factorialize(5);
var factors = arr.reduce(function(previousVal, currentVal) {
  return previousVal * currentVal;
}, 1);

console.log(factors)

答案 5 :(得分:0)

问题是将初始值设置为0而不是乘以数字,而是添加

arr.reduce(callback,initValue)

arr = [1,2,3,4,5]

在提供的代码中,它以下面的格式累积

arr.reduce(function(previousVal, currentVal) {
  return previousVal + currentVal;
}, 0);

第一次通话 - &gt; 0 + 1 = 1(因子= 1)
第二个电话 - &gt; 0 + 2 = 2(因子= 2)
第一次通话 - &gt; 0 + 3 = 3(因子= 3)
第一次通话 - &gt; 0 + 4 = 4(因子= 10)
第一次通话 - &gt; 0 + 5 = 5(因子= 15)

要达到预期效果,请使用以下选项

&#13;
&#13;
var arr = []; // initialize array arr

function factorialize(num) {
//for loop to push 1,2 ,3, 4, 5 to arr array
 for (var i = 1; i <= num; i++) {
  arr.push(i);
}
// return arr.reduce value by multiplying all values in array, default initial value is first element 
  return arr.reduce(function(previousVal, currentVal) {
  //console log to debug and display loop values
  console.log(previousVal, currentVal);
  return previousVal * currentVal;
});
}

console.log("output", factorialize(5));
&#13;
&#13;
&#13;

代码示例 - https://codepen.io/nagasai/pen/YaQKZw?editors=1010