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

294
Visualizações
I do not understand how this JS code is working; how are these two lines being returned?

let specificSuffix = function(words, suffix) {
  if (!Array.isArray(words)) {
    return [];
  }
  return words.filter(function(word) {
    return word.endsWith(suffix)
  });
}

console.log(specificSuffix(['family', 'hound', 'catalyst', 'fly', 'timidly', 'bond'], 'ly'));
// [ 'family', 'fly', 'timidly' ]
console.log(specificSuffix(['simplicity', 'computer', 'felicity'], 'ily'));
// [ ]

I'm confused specifically about:

return words.filter(function(word) {
  return word.endsWith(suffix)
});

How am I returning both of these things? The filter is saying, return this empty new array, fill it with the words that endWith the suffix... (I think)

It's really just the two returns that are throwing me. How does this work?

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Perhaps if we extract the variable result the code will make more sense?

const specificSuffix = function(words, suffix) {
  if (!Array.isArray(words)) return [];

  const result = words.filter(function(word) {
    return word.endsWith(suffix)
  });

  return result;
};
about 4 years ago · Juan Pablo Isaza Relatório

0

return words.filter(function(word){
 return word.endsWith(suffix)
  });

The outer return returns the result of words.filter(). Inside the filter() function we have to pass a callback. So the return inside is for that callback. In short you have to return true/false, based on a condition inside the filter callback.

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