Created
July 7, 2016 22:57
-
-
Save RachelSa/a3cc2ca7fd66323c14ba74782df43832 to your computer and use it in GitHub Desktop.
removes n number of elements from an array, largest to smallest
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
//remove n number of elements from an array, largest to smallest | |
function removeLargest(arr,n){ | |
for (var i = 0; i < n; i ++){ | |
var sorted = Math.max.apply(null, arr); | |
//run through array, funding largest | |
var rem = arr.splice(arr.indexOf(sorted),1); | |
//remove largest | |
} console.log(arr); | |
} | |
removeLargest([1,5,3,6,4],2) | |
removeLargest([-1,-2,3,4,-5,6],3) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment