Skip to content

Instantly share code, notes, and snippets.

@alexko30
Last active May 17, 2018 17:26
Show Gist options
  • Save alexko30/e4e843ac9237ee8079347f3f43451761 to your computer and use it in GitHub Desktop.
Save alexko30/e4e843ac9237ee8079347f3f43451761 to your computer and use it in GitHub Desktop.
Moves some elements of the array to the end
function elemntToTheEnd(arr, el) {
let counter = 0, len = arr.length;
for (let i = 0; i < arr.length; i++) {
counter++;
if (arr[i] == el) {
arr.push(arr[i]);
arr.splice(i, 1);
i--;
}
if (counter == len)
break;
}
return arr;
}
console.log(elemntToTheEnd([7, 2, 3, 0, 4, 6, 0, 0, 13, 0, 78, 0, 0, 19, 14], 13));
// [ 7, 2, 3, 0, 4, 6, 0, 0, 0, 78, 0, 0, 19, 14, 13 ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment