Intermidate algorithm works, but FCC doesnt accept it

Hi!
First, I want to say that the entire FFC is amazing and you guys should all go to tech heaven for supporting this outstading organaization!
second:
I’m at the “Intermediate Algorithm Scripting: Diff Two Arrays”, when I need to return the symmetric difference of the two arrays. The code works, but the FCC checker says it doesnt work in certain exrecises:


function diffArray(arr1, arr2) {

let arr1Obj = {};
let arr2Obj = {};
let unique = [];

for (let i = 0; i < arr1.length; i++) {
arr1Obj[arr1[i]] = arr1Obj[arr1[i]] ? arr1Obj[arr1[i]] +=1 : (arr1Obj[arr1[i]] = 1)
}

for (let j = 0; j < arr2.length; j++) {
arr2Obj[arr2[j]] = arr2Obj[arr2[j]] ? arr2Obj[arr2[j]] +=1 : (arr2Obj[arr2[j]] = 1)
}

for (let key in arr1Obj) {
if (!arr2Obj.hasOwnProperty(key)){
unique.push(key)
}}

for (let key2 in arr2Obj) {
if (!arr1Obj.hasOwnProperty(key2)){
unique.push(key2)
}}

return unique
}

*** diffArray([1, 2, 3, 5], [1, 2, 3, 4, 5]); should retrun 4 - - IT DOES
*** [1, "calf", 3, "piglet"], [1, "calf", 3, 4] should return ["piglet", 4] - - IT DOES
*** [1, "calf", 3, "piglet"], [7, "filly"] should return [1, "calf", 3, "piglet", 7, "filly"] - - IT DOES

The rest works fine.

Thank you very much for your help!

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make easier to read.

See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.

Note: Backticks are not single quotes.

markdown_Forums

1 Like

Your code is not returning the correct values.

diffArray([1, 2, 3, 5], [1, 2, 3, 4, 5]); should return 4 (the number four). What it actually returns is ['4'] (an array containing a string of the character “4”’).

wow… thank you… spend so much time on that simple error…