Skip to content

Instantly share code, notes, and snippets.

@DogPawHat
Created April 8, 2016 15:25
Show Gist options
  • Save DogPawHat/27a603493462f8ac04fe3fb88a04f699 to your computer and use it in GitHub Desktop.
Save DogPawHat/27a603493462f8ac04fe3fb88a04f699 to your computer and use it in GitHub Desktop.
/*Copyright (c) 2016, Ciarán Curley
Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
var arr1 = [1, 2, 3, 4, 5, 6, 7, 8]
var arr2 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
/**
* @param {number[]} arr1
* @param {number[]} arr2
*/
function threeMerge(arr1, arr2) {
/**
* @param {number[]} arr1
* @param {number[]} arr2
* @param {number[]} currentArr
*/
var threeMergeRecursive = function(arr1, arr2, currentArr) {
if (arr1.length < 3) {
return currentArr.concat(arr1, arr2);
} else if (arr2.length < 3) {
var resultArr = currentArr.concat(
arr1.slice(0, 3),
arr2
);
return threeMergeRecursive(
arr1.slice(0, 3),
[],
resultArr
);
} else {
var resultArr = currentArr.concat(
arr1.slice(0, 3),
arr1.slice(0, 3)
);
return threeMergeRecursive(
arr1.slice(3),
arr2.slice(3),
resultArr
);
}
};
return (threeMergeRecursive(arr1, arr2, []));
};
console.log(threeMerge(arr1, arr2));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment