Skip to content

Instantly share code, notes, and snippets.

@o0pmitev
Last active November 21, 2022 04:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save o0pmitev/754ca03aee1fc59fb01aaa2af55a512c to your computer and use it in GitHub Desktop.
Save o0pmitev/754ca03aee1fc59fb01aaa2af55a512c to your computer and use it in GitHub Desktop.
// (3) Convert the following array of arrays in flatten array: [1, 2, 4, 8, 6, 11, 3 , 7]
const toFlatten = [1, 2, [4, 8, [6, 11]], 3, 7];
function flatten(arr) {
let newArray = [];
for(let i = 0; i < arr.length; i++) {
if(typeof(arr[i]) === 'object') {
newArray.push(...flatten(arr[i]));
} else {
newArray.push(arr[i])
}
}
return newArray;
}
/**
* smooth scroll to the bottom of the page
*/
window.scrollTo({ top: document.body.scrollHeight, behavior: 'smooth' })
console.log(flatten(toFlatten))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment