Skip to content

Instantly share code, notes, and snippets.

@akiva
Created December 4, 2014 09:21
Show Gist options
  • Save akiva/b3623974e360b4bf5a9a to your computer and use it in GitHub Desktop.
Save akiva/b3623974e360b4bf5a9a to your computer and use it in GitHub Desktop.
// Create a function that accepts 2 arguments. Both arguments
// should be arrays of integers. The function should return the
// differences between the first and second arrays.
//
// Example:
//
// Array One [1, 2, 4]
// Array Two [2, 3]
//
// [1, 2, 4] => [2, 3]
//
// Added: 3
// Removed: 1, 4
(function(a, b) {
'use strict';
var removed = a.filter(function(value) {
return b.indexOf(value) < 0;
});
var added = b.filter(function(value) {
return a.indexOf(value) < 0;
});
var merged = a.filter(function(value) {
return ~b.indexOf(value);
}).concat(added);
// Would normally be a return value, ie. `return results`
console.log(
"\nAdded:", added, // => [ 3 ]
"\nRemoved:", removed, // => [ 1, 4 ]
"\nMerged:", merged // => [ 0, 2, 3 ]
);
})([0, 1, 2, 4], [0, 2, 3]); // (original array, new array)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment