Last active
May 17, 2018 17:26
-
-
Save alexko30/e4e843ac9237ee8079347f3f43451761 to your computer and use it in GitHub Desktop.
Moves some elements of the array to the end
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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