jyeann
November 18, 2021, 10:25pm
1
Can anyone explain why in the if for the recursion solution,
if return 1 would get 120
if return 2 would get 240
if return 0 get 0 ?
why would the logic would be 0 and not 120??
Tell us what’s happening:
Describe your issue in detail here.
**Your code so far**
function factorialize(num) {
if(num == 0){
return 1;
}
return num * factorialize(num - 1)
}
function factorialize2(num) {
let product = 1;
for(let i = 2; i <= num; i++){
product *= i;
}
return product;
}
function totalNum(num){
if(num === 0){
return 1;
} else {
return num * totalNum(num- 1)
}
}
console.log(((1*2*3*4*5)+0))
console.log(
totalNum(5));
console.log(
factorialize(5));
console.log(
factorialize2(5));
**Your browser information:**
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.82 Safari/537.36
Challenge: Factorialize a Number
Link to the challenge:
The factorial of 1 is 1. The factorial of 2 is 2. So I’m not sure where you are getting 120 and 240? Perhaps I don’t understand exactly what you are asking. Can you try to explain a little more clearly?
The factorial 0 is 1. Again, I’m not sure why you think it would be 120?
Remember that anything times zero is zero (a knowledge bomb ), so if you return zero at the final step as you return back through all the steps it will be “zero * something” or always zero.
jyeann
November 19, 2021, 12:06pm
4
boom!! i see if +0 would do would 15
+1 16, …
*0 0 , 1 120 , 2 240
arrr… thats how it made of. so all return will sum it up !
thanks, you good!
thanks,
i realise the code
function totalNum(num){
if(num === 0){
return 1;
} else {
return num * totalNum(num- 1)
}
return 1 , result 120.
if i change to return2 , in the console it show 240.
system
Closed
May 21, 2022, 12:07am
5
This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.