Finding symmetric difference between two arrays
Clash 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]));
javascript algorithm
add a comment |Â
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]));
javascript algorithm
Replace the if statement withreturn arr1.indexOf(x) == -1 || arr2.indexOf(x) == -1;
. Sincex
might be0
, and it's a falsy value, you'll end up filtering all zeroes as well.
â Ori Drori
Apr 7 at 18:29
add a comment |Â
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]));
javascript algorithm
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]));
javascript algorithm
edited Apr 6 at 18:50
200_success
123k14142399
123k14142399
asked Apr 6 at 17:37
user2763557
23114
23114
Replace the if statement withreturn arr1.indexOf(x) == -1 || arr2.indexOf(x) == -1;
. Sincex
might be0
, and it's a falsy value, you'll end up filtering all zeroes as well.
â Ori Drori
Apr 7 at 18:29
add a comment |Â
Replace the if statement withreturn arr1.indexOf(x) == -1 || arr2.indexOf(x) == -1;
. Sincex
might be0
, 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
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
0
down vote
Your code breaks when one of the arrays contains a 0
element.
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
add a comment |Â
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.
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
add a comment |Â
up vote
0
down vote
Your code breaks when one of the arrays contains a 0
element.
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
add a comment |Â
up vote
0
down vote
up vote
0
down vote
Your code breaks when one of the arrays contains a 0
element.
Your code breaks when one of the arrays contains a 0
element.
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
add a comment |Â
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
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Replace the if statement with
return arr1.indexOf(x) == -1 || arr2.indexOf(x) == -1;
. Sincex
might be0
, and it's a falsy value, you'll end up filtering all zeroes as well.â Ori Drori
Apr 7 at 18:29