I don't know how we got 24 | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

I don't know how we got 24

The code is like this: function p(m){ If(m ==1) { return m; } Else { return m * p(m -1) } } alert (p(4)); I used maths like m=4, m*p(m-1) =4*(4-1), which gives me 12 and it is wrong

17th Mar 2019, 2:54 PM
Xolisile Ndlovu
2 Answers
+ 4
Handtrace it! :> p(4) 4 * p(3) 4 * 3 * p(2) 4 * 3 * 2 * p(1) 4 * 3 * 2 * 1 = 24 You were halfway there but you stopped expanding at p(4-1).
17th Mar 2019, 3:01 PM
Fermi
Fermi - avatar
+ 1
Thank you Fermi
17th Mar 2019, 3:02 PM
Xolisile Ndlovu