I have the following code but it is not giving perfect result for factorial can u find it out plz
You have to return
the value. Here you go:
function fact(x) {
if(x==0) {
return 1;
}
return x * fact(x-1);
}
function run(number) {
alert(fact(parseInt(number, 10)));
}
and
<input type="button" value="Find factiorial" onclick="run(txt1.value)">
(How to make it work for negative numbers I leave up to you ;) (but I showed in this post anyway))
Just for fun, a more correct, non recursive algorithm:
function fact(x) {
if(x == 0) {
return 1;
}
if(x < 0 ) {
return undefined;
}
for(var i = x; --i; ) {
x *= i;
}
return x;
}
<table>
<tr>
<th>Amount of integers</th>
<th>Answer</th>
</tr>
<tr>
<th><input id="int" type="number"/></th>
<th><input id="answer" type="number"/></th>
</tr>
</table>
<button onclick="calculate()">calculate</button>
<script>
function calculate() {
var input = document.getElementById("int").value;
var int = 1;
do {
var product = int *= input;
input--;
} while (input > 0);
answer.value = product;
}
</script>
You first set a table to act as a way to input your variable and have a place to output the answer. You also add a button to execute your function.
The input variable is the value entered by the user. You also have int variable as a placeholder.
Inside the do loop you then another variable that is the product, it takes your placeholder variable and times it by the input. After this the input decrements, as long as input value is then greater than zero the loop keeps iterating.
Then at the end, it posts the answer to the 'answer' id tag in the table.
Here's a short recursive version:
function doFact(n) {
return +!(+(n)) || doFact(n - 1) * n;
}
function factorialFromInput() {
var theInputVal = document.getElementsByTagName("input")[0].value;
var theContainer = document.getElementById("resultContainer");
theContainer.innerHTML = "" + doFact(Math.abs(theInputVal));
}
.wrapper>* {
line-height: 2em;
width: 30%;
}
#resultContainer {
border: outset grey;
min-height: 1.1em;
padding-left: 0.3em;
background-color: #eff0f1;
overflow: scroll;
}
<div class="wrapper">
<input type="text" id="valEntered">
<br>
<button onclick="factorialFromInput();">Calculate Factorial</button>
<br>
<div id="resultContainer"></div>
</div>
function fact(n) {
if (n > 1) {
return n * fact(n-1);
} else {
return 1;
}
}
console.log(fact(5));
Using ternary operator we replace the above code in a single line of code as below
function fact(n) {
return (n != 1) ? n * fact(n - 1) : 1;
}
console.log(fact(5));
This is the very easiest way and latest JS(ES6)
factorial = n => n - 1 > 0 ? n * factorial(n - 1) : n;
//output
console.log(factorial(5));
Here I used ES6 arrow function. For better understanding please see what is arrow function.
<script src="jquery-3.1.0.js"></script>
<script>
$(function () {
var target = 5;
var factorial = 1;
for (var i = 1; i <= target; i++) {
factorial *= i;
}
alert(factorial);
});
</script>
you can set any value in target and this logic will calculate Factorial.
Thanks... :)