Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

129
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda