+ 2

JavaScript Function Challenge

Hello! Can someone please explain this to me? Thank you. //What is the output of this code? function p(m) { if (m==1) { return m; } else { return m * p(m-1); } } alert (p(4));

3rd Jan 2022, 11:06 PM
Tahiti🌍Castillo
Tahiti🌍Castillo - avatar
6 Respostas
+ 2
Solomoni Railoa Thanks a million! This is a very clear and helpful explanation.
3rd Jan 2022, 11:37 PM
Tahiti🌍Castillo
Tahiti🌍Castillo - avatar
+ 1
Solomoni Railoa So if m had been 3, the answer would be 6? And alert (p(5)) would be 120?
3rd Jan 2022, 11:43 PM
Tahiti🌍Castillo
Tahiti🌍Castillo - avatar
+ 1
equal to: let m = 4; for (let mm = m - 1; mm != 1; mm = mm - 1){ m = m * mm; } console.log(m);
3rd Jan 2022, 11:45 PM
FanYu
FanYu - avatar
0
fan yu It looks like an alternate way to figure and output the same number.
3rd Jan 2022, 11:50 PM
Tahiti🌍Castillo
Tahiti🌍Castillo - avatar