Why return undefined, but console.log returns int?

So, I have the following function:

var multiplyT = function(a, b, acc) { if (b == 0) { console.log("BASE CASE: ", acc); return acc; } else { b--; acc = acc + a; console.log("NOT THE BASE CASE: ", a,b,acc); multiplyT(a, b, acc); } } 

It is called using:

 console.log(multiplyT(5,3,0)); 

And gives the following:

 NOT THE BASE CASE: 5 2 5 NOT THE BASE CASE: 5 1 10 NOT THE BASE CASE: 5 0 15 BASE CASE: 15 undefined 

As a way out. I am confused because acc gave the correct value for console.log, but will be "undefined" according to what is returned.

+6
source share
5 answers

This one good. Recursion can make your head spin. The reason it is undefined is that not all iterations return a value, and for those that you don't get undefined, it’s the same as if you set the variable to any function that does not return a value.

This gets confused with recursion because the return value that you see in this case comes from the first and last completed iteration. Unlike a regular method call, when return interrupts the method - sending it back to wherever it appears, recursion still scrolls back through the call stack, returning all the values ​​it should return, including undefined, in reverse order, in which they were called. Thus, it actually gives your console.log four return values: 15, undefined, undefined, undefined.

Since it is synchronous, console.log cannot be executed until the method is processed. What it outputs is the last value it receives, or undefined. If you return after calling the method in the else block, you will see that you got 5 or the acc value after the first iteration of the function.

 var multiplyT = function(a, b, acc) { if (b == 0) { console.log("BASE CASE: ", acc); return acc; } else { b--; acc = acc + a; console.log("NOT THE BASE CASE: ", a,b,acc); multiplyT(a, b, acc); return acc; } } console.log(multiplyT(5,3,0)); 
+5
source

In your else block, this should be return multiplyT(a, b, acc);

+9
source

You also need to return from the else block.

In your case, even if the acc value is updated, the value value is returned when b != 0

 var multiplyT = function(a, b, acc) { if (b == 0) { console.log("BASE CASE: ", acc); return acc; } else { b--; acc = acc + a; console.log("NOT THE BASE CASE: ", a, b, acc); return multiplyT(a, b, acc); //return here } } console.log(multiplyT(5, 3, 0)); 
+5
source

You call the multiplyT function recursively, but you do not control the return. Then multiplyT (5,3,0) does not return a value in the first call, but the function returns undefined.

It is true that the stack is executing, but the first call is more important: it needs to get the value from the recursive internal function that returned the final value.

Correct the code in the else branch to return a recursive call:

 var multiplyT = function(a, b, acc) { if (b == 0) { console.log("BASE CASE: ", acc); return acc; } else { b--; acc = acc + a; console.log("NOT THE BASE CASE: ", a,b,acc); return multiplyT(a, b, acc); } } 
+1
source

I came up with a good solution for this while working on one of my projects in which I traverse a complex JSON object to return some data when searching for an identifier.

 var multiplyT = function(a, b, acc) { if(b == 0) { console.log("BASE CASE : ", acc); return acc; } else { var ret; b--; acc += a; console.log("NOT THE BASE CASE: ",a,b,acc); while( ret = multiplyT(a,b,acc) ) { return ret; } } } 

Here we run the while loop to see if the returned function call returns true or undefined. If it returns something other than undefined , it will return data.

0
source

Source: https://habr.com/ru/post/987706/


All Articles