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

184
Visualizações
How to exclude undefined parameters in a closure?

A JavaScript practice question on closures says the following:

Change the function to support multiple function calls:

function joinWords(a, b) {
    return console.log(a + ' ' + b);
}

See example cases below for example inputs and their expected outputs.

Input 1:
const greet = joinWords("Hello");
greet("world);
Output 1:
"Hello World"

Input 2:
const greet = joinWords("Hey", "there!");
greet("Where", "is", "Foo", "Bar?");
Output 2:
"Hey there! Where is Foo Bar?"

For the solution, I've successfully implemented a closure, but I'm still console logging undefined parameters. AKA, for the first test case (Input 1 and Output 1) I'm outputting Hello undefined World undefined undefined undefined. How can I elegantly exclude undefined parameters here?

function joinWords(a, b) {
    return function(c, d, e, f) {
    console.log(
        a + ' ' + b + ' ' + c + ' ' + d + ' ' + e + ' ' + f
    );
  };
}
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You might be looking for rest parameters, which make the function accept any number of arguments and passes them over as an array (which one can then call the .join method on):

 function joinWords(...values) {
   return values.join(" ");
 }
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