js: function with if-statement | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

js: function with if-statement

function p(m) { if(m==1) { return m; } else { return m*p(m-1); } } console.log(p(4)); // the right output is 24. and i dont understand it. // 4*(4-1)=12 is my opinion. what am i thinking wrong…? 🤔

13th Jul 2021, 8:57 AM
Mylisa_beth
Mylisa_beth - avatar
3 Answers
+ 2
It is a recursive function. It is 4*3*2*1.
13th Jul 2021, 9:08 AM
Paul
Paul - avatar
+ 1
It's executed as 4 * p(3) 4 * 3 * p(2) 4 * 3 * 2 * p(1) 4 * 3 * 2 * 1 24
13th Jul 2021, 9:11 AM
TOLUENE
TOLUENE - avatar
+ 1
thank you!!! 👍
15th Jul 2021, 7:04 AM
Mylisa_beth
Mylisa_beth - avatar