[英]Factorial of a number using a while loop in Javascript
I need help creating the code to find the factorial of a number.我需要帮助创建代码来查找数字的阶乘。 The task is to
任务是
The pseudocode is伪代码是
while(factorial)
if factorial == 0 or factorial == 1
break
result => result * factorial
factorial => factorial - 1
My code below isn't complete because I'm confused by the pseudocode.我下面的代码不完整,因为我对伪代码感到困惑。
function nth_fact(nth){
var a = 1
while(nth_fact)
if (nth_fact == 0 || nth_fact == 1){
break;
result => result * nth_fact
nth_fact => nth - 1
console.log()
}
}
At first lets examine what went wrong:首先让我们检查一下出了什么问题:
var a = 1
What is a
?什么
a
? Its definetly not a good name for a variable.它绝对不是一个变量的好名字。 Maybe name it to
result
?也许将其命名为
result
? The same applies to nth
which should be named factorial
and nth_fact
which should rather be factorize
or sth.这同样适用于应该命名为
factorial
的nth
和应该命名为factorize
或 sth 的nth_fact
。 You should also always use ;
您还应该始终使用
;
to end a statement.结束陈述。
while(nth_fact)
As your while loop contains multiple statements (the if
and the two assignments) you need to open a block here by using {
right after the condition.由于您的 while 循环包含多个语句(
if
和两个赋值),因此您需要在条件之后使用{
在此处打开一个块。 nth_fact
refers to the function, you rather want to take factorial
here. nth_fact
指的是函数,你宁愿在这里取factorial
。
if (nth_fact == 0 || nth_fact == 1){
break;
Now you open a block statement for the if, but you never close it.现在你为 if 打开一个块语句,但你永远不会关闭它。 So you need another
}
after the break.所以休息后你需要另一个
}
。
result => result * nth_fact
nth_fact => nth - 1
console.log()
=>
is the arrow function expression, but you want the assignment operator =
. =>
是箭头函数表达式,但您需要赋值运算符=
。 Also you need to pass something to console.log, eg console.log(result)
您还需要向console.log 传递一些东西,例如
console.log(result)
All together:全部一起:
function factorize(factorial){
var result = 1;
while(factorial){
if (factorial == 0 || factorial == 1){
break;
}
// ?
factorial = factorial - 1;
console.log(result);
}
return result;
}
That pseudocode is indeed confusing, because what it calls factorial
is actually not the factorial -- it's the current value, which the result (which is actually the factorial we're looking for) is multiplied by.该伪代码确实令人困惑,因为它所谓的
factorial
实际上不是阶乘——它是当前值,结果(实际上是我们正在寻找的阶乘)乘以它。 Also, if
is superfluous, because while
already checks for the same condition.另外,
if
是多余的,因为while
已经检查了相同的条件。 So the correct pseudocode would be所以正确的伪代码是
currentValue = argument
factorial = 1
while (currentValue > 1)
factorial = factorial * currentValue
currentValue = currentValue - 1
// now, 'factorial' is the factorial of the 'argument'
Once you get this sorted out, here's a bonus assignment:一旦你解决了这个问题,这是一个奖励任务:
range(a, b)
that creates an array of numbers from a
to b
.range(a, b)
,它创建一个从a
到b
的数字数组。 For example, range(5, 8) => [5, 6, 7, 8]
range(5, 8) => [5, 6, 7, 8]
product(array)
that multiples array elements by each other.product(array)
将数组元素彼此相乘。 For example, product([2, 3, 7]) => 42
product([2, 3, 7]) => 42
product
and range
product
和range
编写阶乘函数 function factorial(num) { var result = 1 while (num) { if ((num) == 0 || (num) == 1) { break; } else { result = result * num; num = num - 1; } } return `The factorial of ${val} is ${result}` } let val = prompt("Please Enter the number : ", "0"); var x = parseInt(val); console.log(factorial(x));
A Short And Clean Code is :一个简短而干净的代码是:
let number = 5; let numberFactorial = number; while(number > 1){ numberFactorial = numberFactorial * (number-1); number--; } console.log(numberFactorial);
I solve this this way我这样解决
function factorial(number) { let num = 1; let result = 1; while (num <= number) { result = result * num; num++; } return result; } const myNumber = factorial(6); console.log(myNumber);
You used the right approach.你使用了正确的方法。 Just the syntax was wrong.
只是语法错误。 Here it is:
这里是:
function nth_fact(nth){
var result = 1 ;
while(nth){
if ((nth) == 0 || (nth) == 1)
break ;
result = result * nth;
nth = nth - 1
}
console.log(result);
return result;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.