Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

192
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda