Skip to content

Instantly share code, notes, and snippets.

@akmalhazim
Created March 9, 2022 14:13
Show Gist options
  • Save akmalhazim/19ff4fad0d044b9e92f6f7d66d75bfe5 to your computer and use it in GitHub Desktop.
Save akmalhazim/19ff4fad0d044b9e92f6f7d66d75bfe5 to your computer and use it in GitHub Desktop.
Algo playground
const arr = [2, [4, 3, [7, 6]]];
const flatArr = [];
const flat = arr => {
if (!Array.isArray(arr)) {
flatArr.push(arr);
return
}
arr.forEach(flat);
}
flat(arr);
for (let i = 0; i < flatArr.length; i++) {
if (flatArr[i] > flatArr[i + 1]) {
const tmp = flatArr[i];
flatArr[i] = flatArr[i + 1];
flatArr[i + 1] = tmp;
}
}
console.log(flatArr)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment