Alright, I'm trying to do one of the freeCodeCamp lessons online and I'm getting undefined when I think that I shouldn't be. I'm new to this so there is probably a really easy solution to this but I would like to know where I am going wrong. The problem involves symmetric differences:
This is what the prompt states: "The mathematical term symmetric difference (△ or ⊕) of two sets is the set of elements which are in either of the two sets but not in both. For example, for sets A = {1, 2, 3} and B = {2, 3, 4}, A △ B = {1, 4}. Symmetric difference is a binary operation, which means it operates on only two elements. So to evaluate an expression involving symmetric differences among three elements (A △ B △ C), you must complete one operation at a time. Thus, for sets A and B above, and C = {2, 3}, A △ B △ C = (A △ B) △ C = {1, 4} △ {2, 3} = {1, 2, 3, 4}. Create a function that takes two or more arrays and returns an array of their symmetric difference. The returned array must contain only unique values (no duplicates)."
Here is my code:
function sym(...args) {
if (args.length == 1) {
console.log(args)
let result = args[0].sort((a, b) => a - b);
console.log(result)
return result;
} else {
let fArray = [];
let aArray = args[0].filter(t => !args[1].includes(t));
let bArray = aArray.concat(args[1].filter(t => !args[0].includes(t)));
bArray.forEach(item => { if (!fArray.includes(item)) fArray.push(item)});
let values = Object.values(args);
values.splice(0, 2, fArray);
sym(...values);
};
};
Is there a reason that the function keeps returning UNDEFINED when I run it? Those two console logs in the IF statement return the correct values that they are looking for, but the RETURN statement will only give undefined...