I want to calculate factorial of a number and show the result to the user.
There is on text field on HTML form. User is entering the number into it whose factorial is to be calculated.
I've written a program for the same and it's working absolutely fine.
But I want to also print the dynamically generated multiplication operation string during factorial calculation.
For example, If user enters 5 in the text field then the output should be like below in a separate text field on a HTML form
5*4*3*2*1=120
and not only simply 120
Following is the code I tried :
<!DOCTYPE HTML>
<html>
<head>
<title> New Document </title>
<script type="text/javascript">
function fact(num)
{
if(num==0)
return 1;
return num* fact(num-1);
}
</script>
</head>
<body>
<form name="f1">
Enter the Number :<input type="text" length="8" name="txt1"><br>
<input type="button" value="Find factiorial" onclick="this.value=fact(parseInt(txt1.value, 10))">
</form>
</body>
</html>
答案 0 :(得分:1)
This is easier if you take a regular loop instead:
function fact(num){
let calc = "" + num, res = num;
while(--num > 0){
res *= num;
calc += " * " + num;
}
return calc + " = " + res;
}
Or if you really want recursion you could pass a touple type:
function fact(n, res = 1, calc = ""){
res *= n;
calc += (calc ? " * ":"") + n;
if(n <= 1)
return [res, calc + " = " + res];
return fact(n - 1, res, calc);
}
答案 1 :(得分:1)
Here's a slightly different approach, which uses iterating functions rather than a recursive one:
function fact(n) {
var numbers = Array.from({length:n}).map(function(_,i) {return i+1;}).reverse();
return numbers.join("*") + "=" + numbers.reduce(function(c,i) {return c*i},1);
}
There's a lot going on with not much code, so here's a breakdown:
Array.from({length:n})
.map(function(_,i) {return i+1;})
[1,2,3,4,5]
.reverse()
[5,4,3,2,1]
numbers.join("*")
*
between each, so 5*4*3*2*1
numbers.reduce(function(c,i) {return c*i},1)
1
). This is an alternative implementation of factorial calculation.答案 2 :(得分:1)
You could take all intermediate results in the function and return at last the formatted result.
This function works recursive with tail optimization.
function fact(num, parts = '', result = 1) {
if (num === 0) {
return (parts || '0!') + ' = ' + result;
}
return fact(num - 1, parts + (parts && ' * ') + num, num * result);
}
<form name="f1">
Enter the Number: <input type="text" length="8" name="txt1"><br>
<input type="button" value="Find factiorial" onclick="this.value = fact(+txt1.value)">
</form>
答案 3 :(得分:0)
试试这个:
<!DOCTYPE HTML>
<html>
<head>
<title> New Document </title>
<script type="text/javascript">
function fact(num){
var result = 1,
str = ""+num+"";
if( num === 0 ){
return 1;
}else{
for(var i = num; i > 0; i--){
result = result * i;
if(i > 1){
str += "*"+(i-1);
}
}
}
answer.innerHTML = str + " = " + result;
}
fact(3);
</script>
</head>
<body>
<form name="f1">
Enter the Number :<input type="number" length="8" name="txt1"><br>
<input type="button" value="Find factiorial" onclick="fact(parseInt(txt1.value, 10))">
</form>
<div id='answer'></div>
</body>
</html>