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

327
Visualizações
Javascript reduce() - why is this returning false?

Basically, I want to evaluate all items in an array. If every item is of equal type AND value (strict equality check) return true, otherwise return false. What am I missing here? - Thanks!

function equalityChecker(arr) {
  return arr.reduce((prev, curr) => {
    if (prev !== curr) {
      return false;
    } 
    if (prev === curr) {
      return true;
    }
  });
}

console.log(equalityChecker([0,0,0,0,0,0])); //returns false
console.log(equalityChecker([1,1,1,1,1,1])); //returns false

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

0

reduce compares the result of the previous test with the current index.

It doesn't compare the previous index with the current index.

(The exception is if you don't pass a value as the second argument to reduce, in which case the first comparison is between the first two indexes).

The first test compares 0 and 0 so the second test compares true and 0, so the third test compares false and false and so on.


When you debugged this you should have added logging which would have shown you what was going on.

function equalityChecker(arr) {
  console.log(arr);
  return arr.reduce((prev, curr) => {
    console.log(`prev: ${prev}, curr: ${curr}`);
    if (prev !== curr) {
      return false;
    }
    if (prev === curr) {
      return true;
    }
  });
}

console.log(equalityChecker([0, 0, 0, 0, 0, 0])); //returns false
console.log(equalityChecker([1, 1, 1, 1, 1, 1])); //returns false

about 4 years ago · Juan Pablo Isaza Relatório

0

Array#reduce returns a value, and you return the result of a comparison.

In the next iteration, you use this boolean value and checks againsta value from the array.

Hint 1: Reduce does not work for this approach.

The other disadvantage is the iteration length, it loops for all elements despite of a false result.

Hint 2: You need a method which returns early.

Result: Take Array#every and compare each element with the first item.

function equalityChecker(array) {
    return array.every((value, _, [first]) => first === value);
}

console.log(equalityChecker([0, 0, 0, 0, 0, 0])); //  true
console.log(equalityChecker([1, 1, 1, 1, 1, 1])); //  true
console.log(equalityChecker([0, 0, 0, 1, 0, 0])); // false

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