Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

186
Vistas
why does this function return ['y','e','s']?

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));

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

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.

about 4 years ago · Juan Pablo Isaza Denunciar

0

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));

about 4 years ago · Juan Pablo Isaza Denunciar

0

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'].

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda