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
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.