-1

can someone please explain it to me how does this function return ['y','e','s'] instead of just the string 'yes' ???? the value of num first will be 8 then else condition will work then num will be [5,6,7] and again the else condition will work then num finally becomes an empty array [] which will fullfill the if condition then it should return 'yes' but it doesn't!?

function ABC(num, ...rest) {
  if (num < 5) {
    return 'yes';
  } else {
    return [...ABC(rest)];
  }
};
console.log(ABC(8, 5, 6, 7));
Jim G.
  • 15,141
  • 22
  • 103
  • 166
xangetsue
  • 15
  • 5
  • Did you do some debugging and inspect the arguments and return values of the recursive calls? – Bergi Feb 28 '22 at 17:03
  • `ABC()` expects a number as its first argument and not an array (of numbers) as in `[...ABC(rest)]` – Andreas Feb 28 '22 at 17:05
  • Well the `else` case returns an array not a string, so I don't see that as surprising. – Bergi Feb 28 '22 at 17:05
  • What do you want the result to be?, seen as none of your number are less than 5, the function seems to have no point. Are you trying to use recursion to create the most inefficient way to find out if any of the numbers is less than 5? – Keith Feb 28 '22 at 17:23
  • @Bergi Yes I did, but I am a bit confused why the function didn't stop at the if block return.. – xangetsue Mar 01 '22 at 12:49
  • @Andreas Actually I saw the code somewhere on the web and I wondered how come the function spitted out this result.. that's why I came here for the sake of learning – xangetsue Mar 01 '22 at 12:51
  • @xangetsue You're calling the function three times. It stops at different points with different values each time. – Bergi Mar 01 '22 at 13:11
  • @Bergi thanks I finally understood thanks for your time – xangetsue Mar 01 '22 at 16:51

3 Answers3

1

When num < 5 it returns yes.

If the first argument doesn't trigger that, they that yes will be returned to:

return [...ABC(rest)];

Where it will be spread and put into an array.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
1

When you make a recursive call, you have [...ABC(rest)] which first runs the function, then spreads its result into an array. This is clearer if we add an intermediate variable:

let recursive_result = ABC(rest);
return [...recursive_result];

What you intended to do was first spread rest into the arguments to the function, and then return the result directly:

let recursive_result = ABC(...rest);
return recursive_result;

Fixing that, we find another problem, which is that once the array is empty, the function will be called with no arguments, and num will be undefined, which is still not "less than 5". That means the if statement will never be reached, and we'll keep recursing forever (until your JS engine gives up and raises an error).

Detecting undefined explicitly, we get the result you expected:

function ABC(num, ...rest) {
  if (num < 5 || typeof num == 'undefined' ) {
    return 'yes';
  } else {
    return ABC(...rest);
  }
};
console.log(ABC(8, 5, 6, 7));
IMSoP
  • 89,526
  • 13
  • 117
  • 169
  • appreciate your help @IMSoP thanks a lot :) – xangetsue Mar 01 '22 at 16:50
  • @xangetsue You can indicate that any question or answer you find on the site was useful to you by clicking the "upvote" button (upwards pointing arrow above the score on the left). Once you have earned enough reputation, you will also be able to mark _unhelpful_ questions and answers by _downvoting_ them. – IMSoP Mar 01 '22 at 16:52
  • noted and you guys deserve more than that believe me ... god bless u all – xangetsue Mar 01 '22 at 17:44
1

If you add a few console.log it becomes clearer :

function ABC(num, ...rest) {
    console.log(`num=`, num, ", rest=", rest)
  if (num < 5) {
      console.log(`returning 'yes'`)
    return 'yes';
  } else {
      console.log(`returning [...ABC(${rest})]`)
    return [...ABC(rest)];
  }
};
console.log(ABC(8, 5, 6, 7));
  • Run 1: num=8 , rest=[5, 6, 7]

So it goes into the else and returns [...ABC([5, 6, 7])]. There is only one argument, an array. So at run 2:

  • Run 2 : num=[5, 6, 7] , rest= []

[5, 6, 7] < 5 ? False. So it goes in the else again. It returns [...ABC([])].

  • Run 3 : num=[] , rest= []

[] < 5 ? True, oddly enough. So ABC([]) returns 'yes'.

But then this 'yes' is being returned into the previous function call, that is, [...ABC([])], which becomes [...'yes'], which is ['y', 'e', 's'].

Jeremy Thille
  • 26,047
  • 12
  • 43
  • 63