+ 3

How does this code work?

Code Omar Einea: var a = (function func(num){ if(num==0) return 1; return 1 + func(num-1); })(3); alert(a);

30th Apr 2019, 10:50 AM
Solo
Solo - avatar
9 Answers
+ 12
Understand the return here in recursion Value is not returned to variable a all the time, it will return only after traceback of recursion So assignment in a will happen only once. Tracing the output will be like 1) when num=3 Func(3) Return 1+func(2) 2) now num is 2 Func(2) Return 1+func(1) 3)now num is 1 Func(1) Return 1+func(0) 4)now num is 0 //limiting condition Func(0) Return 1 #this one is returned to the step 3's func(0) Now value returned in step 3 is =>2 This 2 is returned to the step 2's Func(1) Now func(2) will return 3 to step 1's func(2) Now that returned value 3 + 1 =>4 Thats how you obtained 4 now this 4 is assigned to variable a. thats poping answer/returned value uses stack for storing, hence i told you about the stack.
30th Apr 2019, 1:15 PM
Aaditya Deshpande
Aaditya Deshpande - avatar
+ 11
You have wrote a program for recursive addition of 1, Recursion is when, your function calls itself, and the if condition you specified is limiting condition for recursion As you have wrote a function you can call it directly as, var a = function func(para){}(arg); Here the argument will replace the parameter provided, and will execute a function. Coming back to recursion, Once the program started the execution and encountered a return with a function call to itself, then JS interpreter push all the data into stack (a current result), and this cycle goes on continuously till the limiting condition is reached, once the limiting condition is encountered data is poped from stack in the reverse order it is put in (thats why stack is LIFO) and added 1 in the result As you have said Return 1+ func(num-1) //this one will be added. Hence you will get a number incremented by 1 only... You can try execution here Hope that helped 😄 https://code.sololearn.com/W1XQ3uuX2LpY/?ref=app
30th Apr 2019, 11:11 AM
Aaditya Deshpande
Aaditya Deshpande - avatar
+ 10
I have modified the code to print the stack trace as well, Values retuned will be get printed in console Have a look at this code https://code.sololearn.com/W1XQ3uuX2LpY/?ref=app
30th Apr 2019, 2:14 PM
Aaditya Deshpande
Aaditya Deshpande - avatar
+ 10
Good 👍 glad to hear that..!! Vasiliy
30th Apr 2019, 2:30 PM
Aaditya Deshpande
Aaditya Deshpande - avatar
+ 10
I think you wanted to say, "Your code has solved a number of questions" 😂
30th Apr 2019, 2:52 PM
Aaditya Deshpande
Aaditya Deshpande - avatar
+ 3
30th Apr 2019, 2:17 PM
Solo
Solo - avatar
+ 1
Aaditya Deshpande In general, I realized that this is a recursion, but I do not understand how output 4 is obtained. 1: a = function func(3){ if(3==0) return 1; return 1 + func(3-1); }; => a+=2; ? 2: a = function func(2){ if(2==0) return 1; return 1 + func(2-1); }; => a+=1; ? 3: a = function func(1){ if(1==0) return 1; return 1 + func(1-1); }; => a+=1; ? 4: a = function func(0){ if(0==0) return 1; return 1 + func(0-1); }; => a=4;
30th Apr 2019, 11:45 AM
Solo
Solo - avatar
+ 1
Aaditya Deshpande Your code has spawned a number of questions.🙏
30th Apr 2019, 2:39 PM
Solo
Solo - avatar
+ 1
Aaditya Deshpande No, your code solved one question and created others. 😂
30th Apr 2019, 7:25 PM
Solo
Solo - avatar