question about recursion | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

question about recursion

function a(num) { if (num === 0) document.write(20); document.write(num); a(num - 1); } a(5); so I have this code here that I thought the output would be 5432120 but instead, it goes forever. why?

26th Mar 2019, 1:14 AM
Gabriel Junior
Gabriel Junior - avatar
2 Answers
+ 3
You need to pack the bottom two lines into the `else` block, otherwise they will run regardless of whether you hit 0 or not!
26th Mar 2019, 1:34 AM
Schindlabua
Schindlabua - avatar
+ 2
There's no limit so the program will be running forever
26th Mar 2019, 1:16 AM
Juan David Padilla Diaz
Juan David Padilla Diaz - avatar