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

131
Visualizações
The function for comparing arrays with null object does not work

There is a function, it compares 2 arrays for a match, if they match: true, otherwise: false

But the conditions say that the array can have a null object and if it is present in the array, you need to output false. But I cannot understand why my code does not work, I tried to write in different ways, but the output is the same, no matter whether there is null in the array or not

var isSameTree = function(p, q) {
    for(let i = 0; i <= p.length; i++) {
        for(let j = 0; j <= q.length; j++) {
            if(p[i] || q[j] == null) {
                return false
            }
                if(p[i] == q[j]) {
                    return true
                } else {
                    return false   
            }
        }
    }
};
console.log(isSameTree([1,null,2], [1,2]));

Your input
[1,2,3],[1,2,3]

Output
false

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

0

This condition:

if(p[i] || q[j] == null) {

Looks like it should be:

if(p[i] === null || q[j] === null) {

Also, you'll need to count indexes separately from those that iterate over your arrays. So in your first example, i should not increase if null is seen.

Finally, you're using a <= comparison operator against the array-lengths, where I believe you'll need < instead.

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