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

240
Visualizações
How can I solve this problem with array.prototype?

I'm in a little trouble here. Can't really resolve this problem, I've been using this solution:

 Array.prototype.all = function(callback){
    for(let i = 0; i < this.length; i++){
      if(this[i] < 10) return true;
  }
      if(this[i] < 2) return false;
    }
}

But it does not work. I know I'm making a rookie mistake (I'm new to learning javascript), and would love a little help on this.

The problem in question is this one:

function all(fn) {
      // Write a function called all in the Array prototype,
      // that receives a function (callback). Assume fn always returns true or false.
      // The function all must return true if fn returns true when we invoke it passing all the elements of the array one by one
      // the function all must return false, if any element of the array causes fn to return false;
      // E.g:
      // [1,2,3].all(function(elem) {
      //    return elem < 10;
      //  });
      // returns true
      // [1,2,3].all(function(elem) {
      //    return elem < 2;
      //  });
      // returns false.
    }
about 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

You only have to return true if the returned value from callback is false else return true

Array.prototype.all = function (callback) {
    for (let i = 0; i < this.length; ++i) {
        const result = callback(this[i]);
        if (!result) return false;
    }

    return true;
};

const arr1 = [1, 2, 3, 4, 20, 300, 0];
const arr2 = [1, 0];

console.log(arr1.all((elem) => elem < 2));
console.log(arr2.all((elem) => elem < 2));

ADDITIONAL INFO

Array.prototype already has every method that will the exact same thing

const arr1 = [1, 2, 3, 4, 20, 300, 0];
const arr2 = [1, 0];
console.log(arr1.every((elem) => elem < 2));
console.log(arr2.every((elem) => elem < 2));

about 4 years ago · Santiago Trujillo 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