When I use the code the requirements are met, but don’t activate.
my code so far
var h = [] ;
function diffArray(arr1, arr2) {
var gt = arr1.concat(arr2);
for(i = 0; i < gt.length; i++){
var j = gt[i];
var s = arr1.indexOf(j);
if(s == -1){
t.push(j);
}
var r = arr2.indexOf(j);
if(r == -1){
t.push(j);
}}
var m = arr2.filter(o);
var newArr = [];
for(e = 0;e < t.length; e++){
newArr.push(t[e]);
}
// Same, same; but different.
return newArr;
}
function o(value){
return value !== 1;
}
diffArray(["diorite", "andesite", "grass", "dirt", "pink wool", "dead shrub"], ["diorite", "andesite", "grass", "dirt", "dead shrub"]);
https://www.freecodecamp.org/challenges/diff-two-arrays
Your code contains global variables that are changed each time the function is run. This means that after each test completes, subsequent tests start with the new value. To fix this, make sure your function doesn’t change any global variables, and declare/assign variables within the function if they need to be changed.
Example:
var myGlobal = [1];
function returnGlobal(arg) {
myGlobal.push(arg);
return myGlobal;
} // unreliable - array gets longer each time the function is run
function returnLocal(arg) {
var myLocal = [1];
myLocal.push(arg);
return myLocal;
} // reliable - always returns an array of length 2
There’s also this ReferenceError: t is not defined
. Maybe you meant the h
one?
You should also avoid using single letters for variable/function names (except for-loop variables are usually given single-letter names). Give them more descriptive ones. It will help readers (especially yourself) when reading the code and figuring out what it does and whatever’s wrong with it.