Diff Two Arrays mistery

Diff Two Arrays mistery
0.0 0

#1

Tell us what’s happening:
hy! can someone help me find the problem … thaaanks in advance:)

Your code so far

function diffArray(arr1, arr2) {
 
  
  /* var mix = arr1.concat(arr2);  
  
  var notTwice = mix.filter(function(){
     for (i=0; i<=mix.length; i++)    {
       if (arr1.includes(i) === true || arr2.includes(i) === true ) {
           return i;
     }
     }
        }); 
  
  return notTwice; */
  
   var mix = arr1.concat(arr2);
  
  var notTwice = mix.filter(function(){
     for (i=0; i<=mix.length; i++){
       if (arr1.includes(mix.indexOf(i)) === false & arr2.includes(mix.indexOf(i)) === false ) {
           return i;
     }
     }
        }); 
  
  return notTwice; 
  
  
  
  
} 
 

diffArray([1, 2, 3, 5], [1, 2, 3, 4, 5]);

Your browser information:

Your Browser User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36.

Link to the challenge:


#2

the callback function of the Array.filter must return true of false.
the filtered array will be the element if true returned in it.

Array.filter ( function() { 
    return true; // or false; 
} );

#4

yeah, right! thanks for the tip!!

function diffArray(arr1, arr2) {

var mix = arr1.concat(arr2);

var notTwice = mix.filter(function(){
for (i=0; i<=mix.length; i++){
if (arr1.includes(mix.indexOf(i)) === true & arr2.includes(mix.indexOf(i)) === true ) {
return false;
}
}
});

return notTwice;

}

i still got an empty array…:frowning:


#5

The Array.filter() method loops through your array elements, you don’t have to put another loop in there. For each array element, the callback passes its value, its index and the original array as arguments like this:

arr.filter(function (element, index, arr) {
   // do stuff here
});

so you can use the callback index argument to do your calculations.

Also, you only return false in your array.filter() right now. As @skim0905 mentioned, you either need to return true or false. Unless you return true for some elements, your notTwice array is going to be empty.

Lastly, you have a syntax error in your if statement. The operator is && not &.


#6

function diffArray(arr1, arr2) {
    // if assuming, alphanumeric only,,and no duplicate value in same array,, 
    const dic = "1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
    var alphanumeric = Array(62); // 26+26+10
    var mix = arr1.concat(arr2);
    
    for(var i=0; i<mix.length; i++){
        var pos = dic.indexOf(mix[i]);
        if(!alphanumeric[pos])   
            alphanumeric[pos] = true;
        else 
            alphanumeric[pos] = !alphanumeric[pos]; 
    }
    // find any element true,, 
    var notTwice = alphanumeric.filter(function(val){ 
        if(!val)
            return false;
        
        return val;
    }); 

    return notTwice; 
}