Skip to content

Instantly share code, notes, and snippets.

@kolyuchii
Created January 2, 2020 10:31
Show Gist options
  • Select an option

  • Save kolyuchii/e40acaa9145fc0811076848866ede965 to your computer and use it in GitHub Desktop.

Select an option

Save kolyuchii/e40acaa9145fc0811076848866ede965 to your computer and use it in GitHub Desktop.
function find(arr) {
const maxDepth = arr.length - 1;
const result = [];
function helper(newArray, arrayIndex) {
for (let i = 0; i < arr[arrayIndex].length; i += 1) {
const arrCopy = newArray.slice(0);
arrCopy.push(arr[arrayIndex][i]);
if (arrayIndex === maxDepth) {
result.push(arrCopy);
} else {
helper(arrCopy, arrayIndex + 1);
}
}
}
helper([], 0);
return result;
}
find([[1,2], [3,4], [5,6]]) // [1,3,5] [1,3,6] [1,4,6] [2,3,5] [2,4,5] [2,4,6]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment