Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

130
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!