javascript 对数字进行因式分解
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32147732/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Factorialize a Number
提问by VADO
I'm taking the freecodecamp course one of the exercises it's to create a Factorialize
function, I know there is several ways to do it just not sure what this one keeps returning 5
我正在参加 freecodecamp 课程,其中一项练习是创建一个Factorialize
函数,我知道有几种方法可以做到,只是不确定这个不断返回什么5
function factorialize(num) {
var myMax = num;
var myCounter = 1;
var myTotal = 0;
for (i = 0; i>= myMax; i++) {
num = myCounter * (myCounter + 1);
myCounter++;
}
return num;
}
factorialize(5);
回答by Andro
This is a recursive solution of your problem:
这是您问题的递归解决方案:
function factorialize(num) {
if(num <= 1) {
return num
} else {
return num * factorialize(num-1)
}
}
factorialize(5)
This is the iterative solution:
这是迭代解决方案:
function factorialize(num) {
var cnt = 1;
for (var i = 1; i <= num ; i++) {
cnt *= i;
}
return cnt;
}
factorialize(5)
with argument 5, it will return the 5! or 120.
使用参数 5,它将返回 5!或 120。
回答by Koleen BP
My solution in compliance with convention for empty product
我的符合空产品公约的解决方案
function factorializer(int) {
if (int <= 1) {
return 1;
} else {
return int * factorializer(int - 1);
}
}
回答by Hans Mikesen
To answer your question, why your function is returning 5: Your function never reaches the inner part of the for-loop because your testing if i is greater than myMax instead of less than. So you are just returning your input parameter which is five.
要回答您的问题,为什么您的函数返回 5:您的函数永远不会到达 for 循环的内部部分,因为您测试 i 是否大于 myMax 而不是小于。所以你只是返回你的输入参数,它是 5。
But the loop does not calculate the factorial of num, it only multiplies (num+1) with (num+2);
但是循环不计算 num 的阶乘,它只是将 (num+1) 与 (num+2) 相乘;
回答by Ahmad Saleh
Here is another way to solve this challenge and I know it is neither the shortest nor the easiest but it is still a valid way.
这是解决这一挑战的另一种方法,我知道它既不是最短的也不是最简单的,但它仍然是一种有效的方法。
function factorialiaze(num){
var myArr = []; //declaring an array.
if(num === 0 || num === 1){
return 1;
}
if (num < 0){ //for negative numbers.
return "N/A";
}
for (var i = 1; i <= num; i++){ // creating an array.
myArr.push(i);
}
// Reducing myArr to a single value via .reduce:
num = myArr.reduce(function(a,b){
return a * b;
});
return num;
}
factorialiaze(5);
回答by Imran Khan
Try this function
试试这个功能
const factorialize = (num) => num === 0 ? 1 : num * factorialize(num-1)
Use it like this:
像这样使用它:
factorialize(5) // returns 120
回答by Marina ES
Try this :
试试这个 :
function factorialize(num) {
var value = 1;
if(num === 1 || num ===0) {
return value;
} else {
for(var i = 1; i<num; i++) {
value *= i;
}
return num * value;
}
}
factorialize(5);
回答by Nina Scholz
Maybe you consider another approach.
也许你考虑另一种方法。
This solution features a very short - cut to show what is possible to get with an recursive style and a implicit type conversion:
此解决方案具有一个非常快捷的功能,以展示使用递归样式和隐式类型转换可能获得的结果:
function f(n) { return +!~-n || n * f(n - 1); }
- + convert to number
- ! not
- ~ not bitwise
- - negative
- + 转换为数字
- !不是
- ~ 不是按位
- - 消极的
function f(n) { return +!~-n || n * f(n - 1); }
var i;
for (i = 1; i < 20; i++) {
console.log(f(i));
}
.as-console-wrapper { max-height: 100% !important; top: 0; }