JSFiddle - React, Tailwind, and code Playground

by Eugen Sunic

JavaScript

const list = [
  [1, 2, 3, 5],
]

/* const sub = [1, 2, 3];
list.forEach(subList => {
  const isSame = subList.every((x, i) => sub[i] === x);
  if (isSame) {
    console.log('true')
  }
}) */

const parentArray = [];

function swap(a, b) {
  const temp = a;
  a = b;
  b = temp;
}

var permute = function(nums) {
  // iterate with indexes
  if (parentArray.length === 6) {
    return parentArray;
  }
  for (let i = 0; i < nums.length - 1; i++) {
    swap(list[i], list[i + 1]);
		console.log('never', parentArray)
    parentArray.push(nums);
    permute(nums);
    swap(list[i], list[i + 1]);
  }
}

console.log(permute([1, 2, 3]));


/*     swap on the index, recurse, swap back
    base
    case if lenght of the 2 d is equal to 6 then get out in the end check
    if the sub exists in the 2 d array
     */