Created
November 29, 2015 22:27
-
-
Save codebubb/916affa747d509253974 to your computer and use it in GitHub Desktop.
This file contains hidden or 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: Sorted Union | |
| // Author: @codebubb | |
| // Challenge: http://www.freecodecamp.com/challenges/bonfire-sorted-union?solution=function%20unite()%20%7B%0A%20%20%2F%2FImportant%3A%20You%20should%20not%20slice%20on%20arguments%20because%20it%20prevents%20optimizations%20in%20JavaScript%20engines%20(V8%20for%20example).%20Instead%2C%20try%20constructing%20a%20new%20array%20by%20iterating%20through%20the%20arguments%20object%0A%20%20args%20%3D%20new%20Array(arguments.length)%3B%0A%20%20for(var%20i%3D0%3B%20i%3Carguments.length%3B%20i%2B%2B)%7B%0A%20%20%20%20args%5Bi%5D%20%3D%20arguments%5Bi%5D%3B%0A%20%20%7D%0A%20%20args%20%3D%20args.reduce(function(a%2Cb)%7B%0A%20%20%20%20return%20a.concat(b)%3B%0A%20%20%7D)%3B%0A%20%20return%20args.filter(function(a%2Ci)%7B%0A%20%20%20%20return%20args.indexOf(a)%20%3D%3D%20i%3B%0A%20%20%7D)%3B%0A%7D%0A%0A%0A%0A%0Aunite(%5B1%2C%203%2C%202%5D%2C%20%5B5%2C%202%2C%201%2C%204%5D%2C%20%5B2%2C%201%5D)%3B%0A | |
| // Learn to Code at Free Code Camp (www.freecodecamp.com) | |
| function unite() { | |
| //Important: You should not slice on arguments because it prevents optimizations in JavaScript engines (V8 for example). Instead, try constructing a new array by iterating through the arguments object | |
| args = new Array(arguments.length); | |
| for(var i=0; i<arguments.length; i++){ | |
| args[i] = arguments[i]; | |
| } | |
| args = args.reduce(function(a,b){ | |
| return a.concat(b); | |
| }); | |
| return args.filter(function(a,i){ | |
| return args.indexOf(a) == i; | |
| }); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment