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

103
Visualizações
Javascript return more than once?

please can you help me understand when and under what conditions should I be returning more than once in a statement block? for example, my code:

function checkPositive(arr) {
    return arr.every(function(element) {
        return element > 0;
   });
}

why does this only work if I return twice?

I'm confused about how to follow this structure and when to know I should be using them.

Thanks

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

0

Since your code has two functions, and a return only applies to the function that return is a member of, you need two return statements.

It may become more clear when you first define the inner function and then the outer function:

function isPositive(element) {
    return element > 0;
}
function checkPositive(arr) {
    return arr.every(isPositive);
}

The only difference here, is that we have given a name to the inner function (isPositive) using a function statement.

In this particular case, the isPositive function returns whether a single value is positive, while checkPositive returns whether all values are positive. These functions have a different purpose, and thus are responsible for returning what they are intended to evaluate.

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