Skip to content

Instantly share code, notes, and snippets.

@rubenribeiro
Forked from anonymous/bonfire-sorted-union#.js
Created December 6, 2015 23:55
Show Gist options
  • Save rubenribeiro/7b02b9b0e24bcb0e88a1 to your computer and use it in GitHub Desktop.
Save rubenribeiro/7b02b9b0e24bcb0e88a1 to your computer and use it in GitHub Desktop.

Revisions

  1. @invalid-email-address Anonymous created this gist Dec 6, 2015.
    27 changes: 27 additions & 0 deletions bonfire-sorted-union#.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,27 @@
    // Bonfire: Sorted Union
    // Author: @dannycoder
    // Challenge: http://www.freecodecamp.com/challenges/bonfire-sorted-union#
    // Learn to Code at Free Code Camp (www.freecodecamp.com)

    function unite(arr1, arr2, arr3) {
    var newArray = [];

    for(var i = 0; i < arguments.length; i++) {
    for (var j = 0; j < arguments[i].length; j++)
    {
    //add first element to array
    if( i === 0 && j === 0) {
    newArray.push(arguments[i][j]);
    continue;
    }
    //add next element if not found in the @newArray
    if(newArray.indexOf(arguments[i][j]) === -1){
    newArray.push(arguments[i][j]);
    console.log(arguments[i][j]);
    }
    }
    }
    return newArray;
    }

    unite([1, 3, 2], [5, 2, 1, 4], [2, 1]);