-
Notifications
You must be signed in to change notification settings - Fork 340
Bonfire Diff Two Arrays
Rafael J. Rodriguez edited this page Jan 6, 2017
·
2 revisions
Created by Rafase282
Github | FreeCodeCamp | CodePen | LinkedIn | Website | E-Mail
- Difficulty: 2/5
Compare two arrays and return a new array with any items not found in both of the original arrays.
Remember to use Read-Search-Ask if you get stuck. Try to pair program. Write your own code.
function diff(arr1, arr2) {
var newArr = [];
// Same, same; but different.
return newArr;
}
diff([1, 2, 3, 5], [1, 2, 3, 4, 5]);
- You will check two arrays and return a new array that contains only the items that are not in either of the original arrays.
- Merge the list to make it easy to compare functions.
- Use filter to get the new array, you will need to create a callback function.
- The best way to go about the callback function is to check if the number from the new merged array is not in both original arrays and return it.
Solution ahead!
function diff(arr1, arr2) {
var newArr = arr1.concat(arr2);
function check(item) {
if (arr1.indexOf(item) === -1 || arr2.indexOf(item) === -1) {
return item;
}
}
return newArr.filter(check);
}
- Merge both arrays on the new one using
.concat()
- Create a callback function to use with filter. The function will check if the number is not present in both original arrays and return it.
- Return the filtered array.
Thanks for visiting, if you like this please feel free to star my repo, follow me or even contact me about contributing as it will be a lot of work and having help would be cool.
- HTML5 and CSS
- Responsive Design with Bootstrap
- Gear up for Success
- jQuery
- Basic JavaScript
- Object Oriented and Functional Programming
- Basic Algorithm Scripting
- Basic Front End Development Projects
- Intermediate Algorithm Scripting
- JSON APIs and Ajax
- Intermediate Front End Development Projects
- Claim Your Front End Development Certificate
- Upper Intermediate Algorithm Scripting
- Automated Testing and Debugging
- Advanced Algorithm Scripting
- AngularJS (Legacy Material)
- Git
- Node.js and Express.js
- MongoDB
- API Projects
- Dynamic Web Applications
- Claim Your Back End Development Certificate
- Greefield Nonprofit Project 1
- Greefield Nonprofit Project 2
- Legacy Nonprofit Project 1
- Legacy Nonprofit Project 2
- Claim your Full Stack Development Certification
- Whiteboard Coding Interview Training
- Critical Thinking Interview Training
- Mock Interview 1
- Mock Interview 2
- Mock Interview 3