My code run properly in my editor(Atom) but got error when tested by Freecodecamp, error message: arr.filter is not a function
You can have a look at my code:
/*
Symmetric Difference
U(A,B) - n(A,B)
*/
function uniqueArr(arr) {
return arr.filter(function(item, pos){
return arr.indexOf(item) == pos;
});
}
function interset(arr1, arr2) {
var union = [];
var uniqueArr1 = uniqueArr(arr1);
var uniqueArr2 = uniqueArr(arr2);
union = arr1.filter(function(value){
if (uniqueArr2.indexOf(value) > -1) {
return value;
}
})
return union;
}
function sym(args) {
var newArr = [];
for (var key in arguments) {
newArr.push(arguments[key]);
}
var concatedArr = newArr.reduce(function(a, b){
var uniqueArr1 = uniqueArr(a);
var uniqueArr2 = uniqueArr(b);
var intersetNums = interset(uniqueArr1, uniqueArr2);
return uniqueArr1.concat(uniqueArr2).filter(function(value) {
if (intersetNums.indexOf(value) < 0) {
return value;
}
})
})
return concatedArr.sort(function(a, b) {
return a - b;
});
}
console.log(sym([1, 2, 3], [5, 2, 1, 4]));
Any idea why, thanks?
So, that’s odd. I just tried your code on codepen and then in the challenge and it passed just fine. (although it yelled about a couple missing semicolons.)
Maybe you had a typo or a stray extra character?
Try clearing the challenge field and then pasting in the code you pasted here.
I think I found your problem! Are you using Firefox?
Thanks for your replay. I just Reset the challenge and pasted the code again but it sill doesn’t work, quite strange.
1 Like
No, I use safari~ Should I give chrome a try?
Safari Might have the same issue as Firefox: as an experiment. try moving your function definitions above the sym function.
(Firefox will sometimes yell at your when you call a function that wasn’t defined above where you called it.)
Wow~ I tried in Chrome and it passed . But it won’t pass in Safari even I moved uniqueArr and interest above sum as you suggest, possible a bug?
probably more in how Safari interprets JS. If you want to keep experimenting, I’d try putting them inside sym and see what happens.
1 Like
I thought I’d download Safari and give it a try myself, but apparently there isn’t a current version for Windows.
Just have a try, I put these two help functions inside sym, but the issue still exist.
Odd.
I have no idea then. Maybe there’s a mac user that would know.
I tried it in Safari 9 and it works perfectly.
1 Like
I got a similar message but the problematic “array” was arguments. I used Array.from(arguments) instead.