Finding symmetric difference between two arrays

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
-4
down vote

favorite












In this assignment from freecodecamp, I was given the following mdn resources:
1. slice()
2. filter()
3. indexOf()
4. concat()



While yes, i could do this using for loop and other methods, i stuck to what was given to me to figure out the problem. Wondering if I could have done something better/efficient with my code using the resources above.






function diffArray(arr1, arr2) 
var newArr = ;

var concatArr = arr1.concat(arr2);

return newArr = concatArr.filter(function(x)


if (arr1.indexOf(x) == -1 );




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









share|improve this question





















  • Replace the if statement with return arr1.indexOf(x) == -1 || arr2.indexOf(x) == -1;. Since x might be 0, and it's a falsy value, you'll end up filtering all zeroes as well.
    – Ori Drori
    Apr 7 at 18:29

















up vote
-4
down vote

favorite












In this assignment from freecodecamp, I was given the following mdn resources:
1. slice()
2. filter()
3. indexOf()
4. concat()



While yes, i could do this using for loop and other methods, i stuck to what was given to me to figure out the problem. Wondering if I could have done something better/efficient with my code using the resources above.






function diffArray(arr1, arr2) 
var newArr = ;

var concatArr = arr1.concat(arr2);

return newArr = concatArr.filter(function(x)


if (arr1.indexOf(x) == -1 );




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









share|improve this question





















  • Replace the if statement with return arr1.indexOf(x) == -1 || arr2.indexOf(x) == -1;. Since x might be 0, and it's a falsy value, you'll end up filtering all zeroes as well.
    – Ori Drori
    Apr 7 at 18:29













up vote
-4
down vote

favorite









up vote
-4
down vote

favorite











In this assignment from freecodecamp, I was given the following mdn resources:
1. slice()
2. filter()
3. indexOf()
4. concat()



While yes, i could do this using for loop and other methods, i stuck to what was given to me to figure out the problem. Wondering if I could have done something better/efficient with my code using the resources above.






function diffArray(arr1, arr2) 
var newArr = ;

var concatArr = arr1.concat(arr2);

return newArr = concatArr.filter(function(x)


if (arr1.indexOf(x) == -1 );




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









share|improve this question













In this assignment from freecodecamp, I was given the following mdn resources:
1. slice()
2. filter()
3. indexOf()
4. concat()



While yes, i could do this using for loop and other methods, i stuck to what was given to me to figure out the problem. Wondering if I could have done something better/efficient with my code using the resources above.






function diffArray(arr1, arr2) 
var newArr = ;

var concatArr = arr1.concat(arr2);

return newArr = concatArr.filter(function(x)


if (arr1.indexOf(x) == -1 );




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








function diffArray(arr1, arr2) 
var newArr = ;

var concatArr = arr1.concat(arr2);

return newArr = concatArr.filter(function(x)


if (arr1.indexOf(x) == -1 );




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





function diffArray(arr1, arr2) 
var newArr = ;

var concatArr = arr1.concat(arr2);

return newArr = concatArr.filter(function(x)


if (arr1.indexOf(x) == -1 );




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








share|improve this question












share|improve this question




share|improve this question








edited Apr 6 at 18:50









200_success

123k14142399




123k14142399









asked Apr 6 at 17:37









user2763557

23114




23114











  • Replace the if statement with return arr1.indexOf(x) == -1 || arr2.indexOf(x) == -1;. Since x might be 0, and it's a falsy value, you'll end up filtering all zeroes as well.
    – Ori Drori
    Apr 7 at 18:29

















  • Replace the if statement with return arr1.indexOf(x) == -1 || arr2.indexOf(x) == -1;. Since x might be 0, and it's a falsy value, you'll end up filtering all zeroes as well.
    – Ori Drori
    Apr 7 at 18:29
















Replace the if statement with return arr1.indexOf(x) == -1 || arr2.indexOf(x) == -1;. Since x might be 0, and it's a falsy value, you'll end up filtering all zeroes as well.
– Ori Drori
Apr 7 at 18:29





Replace the if statement with return arr1.indexOf(x) == -1 || arr2.indexOf(x) == -1;. Since x might be 0, and it's a falsy value, you'll end up filtering all zeroes as well.
– Ori Drori
Apr 7 at 18:29











1 Answer
1






active

oldest

votes

















up vote
0
down vote













Your code breaks when one of the arrays contains a 0 element.






share|improve this answer





















  • how would i solve that?
    – user2763557
    Apr 7 at 15:54










  • Sorry, Code Review is not a debugging service.
    – 200_success
    Apr 7 at 18:09










  • Go to www.stackoverflow.com if you want to ask a question on programming. This site is for code review. @user2763557
    – Colea
    Apr 7 at 18:29











Your Answer




StackExchange.ifUsing("editor", function ()
return StackExchange.using("mathjaxEditing", function ()
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
);
);
, "mathjax-editing");

StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "196"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);








 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f191426%2ffinding-symmetric-difference-between-two-arrays%23new-answer', 'question_page');

);

Post as a guest






























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote













Your code breaks when one of the arrays contains a 0 element.






share|improve this answer





















  • how would i solve that?
    – user2763557
    Apr 7 at 15:54










  • Sorry, Code Review is not a debugging service.
    – 200_success
    Apr 7 at 18:09










  • Go to www.stackoverflow.com if you want to ask a question on programming. This site is for code review. @user2763557
    – Colea
    Apr 7 at 18:29















up vote
0
down vote













Your code breaks when one of the arrays contains a 0 element.






share|improve this answer





















  • how would i solve that?
    – user2763557
    Apr 7 at 15:54










  • Sorry, Code Review is not a debugging service.
    – 200_success
    Apr 7 at 18:09










  • Go to www.stackoverflow.com if you want to ask a question on programming. This site is for code review. @user2763557
    – Colea
    Apr 7 at 18:29













up vote
0
down vote










up vote
0
down vote









Your code breaks when one of the arrays contains a 0 element.






share|improve this answer













Your code breaks when one of the arrays contains a 0 element.







share|improve this answer













share|improve this answer



share|improve this answer











answered Apr 6 at 18:50









200_success

123k14142399




123k14142399











  • how would i solve that?
    – user2763557
    Apr 7 at 15:54










  • Sorry, Code Review is not a debugging service.
    – 200_success
    Apr 7 at 18:09










  • Go to www.stackoverflow.com if you want to ask a question on programming. This site is for code review. @user2763557
    – Colea
    Apr 7 at 18:29

















  • how would i solve that?
    – user2763557
    Apr 7 at 15:54










  • Sorry, Code Review is not a debugging service.
    – 200_success
    Apr 7 at 18:09










  • Go to www.stackoverflow.com if you want to ask a question on programming. This site is for code review. @user2763557
    – Colea
    Apr 7 at 18:29
















how would i solve that?
– user2763557
Apr 7 at 15:54




how would i solve that?
– user2763557
Apr 7 at 15:54












Sorry, Code Review is not a debugging service.
– 200_success
Apr 7 at 18:09




Sorry, Code Review is not a debugging service.
– 200_success
Apr 7 at 18:09












Go to www.stackoverflow.com if you want to ask a question on programming. This site is for code review. @user2763557
– Colea
Apr 7 at 18:29





Go to www.stackoverflow.com if you want to ask a question on programming. This site is for code review. @user2763557
– Colea
Apr 7 at 18:29













 

draft saved


draft discarded


























 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f191426%2ffinding-symmetric-difference-between-two-arrays%23new-answer', 'question_page');

);

Post as a guest













































































Popular posts from this blog

Chat program with C++ and SFML

Function to Return a JSON Like Objects Using VBA Collections and Arrays

Will my employers contract hold up in court?