Forked from anonymous/bonfire-diff-two-arrays?solution=function%20diff(arr1%2C%20arr2)%20%7B%0A%20%20var%20newArr%20%3D%20%5B%5D%3B%0A%20%20var%20t%20%3D%200%3B%0A%20%20for(var%20i%20%3D%200%3B%20i%20%3C%20arr1.length%3B%20i%2B%2B)%7B%0A%20%20%20%20%20%20if(arr2.indexOf(a
Created
November 20, 2015 18:19
-
-
Save radumazilu/43faf23cb96d4e570538 to your computer and use it in GitHub Desktop.
http://www.freecodecamp.com/radumazilu 's solution for Bonfire: Diff Two Arrays
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Bonfire: Diff Two Arrays | |
// Author: @radumazilu | |
// Challenge: http://www.freecodecamp.com/challenges/bonfire-diff-two-arrays?solution=function%20diff(arr1%2C%20arr2)%20%7B%0A%20%20var%20newArr%20%3D%20%5B%5D%3B%0A%20%20var%20t%20%3D%200%3B%0A%20%20for(var%20i%20%3D%200%3B%20i%20%3C%20arr1.length%3B%20i%2B%2B)%7B%0A%20%20%20%20%20%20if(arr2.indexOf(arr1%5Bi%5D)%20%3D%3D%3D%20-1)%0A%20%20%20%20%20%20%20%20newArr.push(arr1%5Bi%5D)%3B%0A%20%20%20%20%20%20%7D%0A%20%20%20%20for(var%20j%20%3D%200%3B%20j%20%3C%20arr2.length%3B%20j%2B%2B)%7B%0A%20%20%20%20%20%20if(arr1.indexOf(arr2%5Bj%5D)%20%3D%3D%3D%20-1)%0A%20%20%20%20%20%20%20%20newArr.push(arr2%5Bj%5D)%3B%0A%20%20%20%20%20%20%7D%0A%20%20%2F%2F%20Same%2C%20same%3B%20but%20different.%0A%20%20return%20newArr%3B%0A%7D%0A%0Adiff(%5B1%2C%202%2C%203%2C%205%5D%2C%20%5B1%2C%202%2C%203%2C%204%2C%205%5D)%3B%0A | |
// Learn to Code at Free Code Camp (www.freecodecamp.com) | |
function diff(arr1, arr2) { | |
var newArr = []; | |
var t = 0; | |
for(var i = 0; i < arr1.length; i++){ | |
if(arr2.indexOf(arr1[i]) === -1) | |
newArr.push(arr1[i]); | |
} | |
for(var j = 0; j < arr2.length; j++){ | |
if(arr1.indexOf(arr2[j]) === -1) | |
newArr.push(arr2[j]); | |
} | |
// Same, same; but different. | |
return newArr; | |
} | |
diff([1, 2, 3, 5], [1, 2, 3, 4, 5]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment