I want to write a function which removes elements from an array of integers starting from the lowest values without changing the positions of the elements. Programming language is ActionScript3.
e.g. (these are individual trace statements)
var aNumArr:Array = [0,7,2,5,9,0]
trace(RemoveMinValues(aNumArr, 1, false)) //output: 7,2,5,9,0
//trace(RemoveMinValues(aNumArr, -1, true)) //output: 0,7,2,5,9
//trace(RemoveMinValues(aNumArr, 2)) //output: 7,2,5,9
I have managed to remove the lowest values in an array using sort(Array.NUMERIC)
and sort(Array.DESCENDING)
.
But I can't seem to figure out how to move the elements back to their original positions.
As this is an assignment, I cannot copy the entire function code. And I wish you not to tell me the exact answer, but rather give me insight on how to go about doing it.
Hope I was clear enough. Please let me know if you need additional info.
Thanks in advance.
EDIT: I realised I missed out a few things on the function. I've also changed the aNumArr
and desired output values to make it clearer what I want.
And here was what I did previously:
function RemoveMinValues(aNumArr:Array, iMinsToRemove:int):void
{
if(iMinsToRemove >= 0)
{
aNumArr.sort(Array.NUMERIC);
for(var i:int = 0; i < iMinsToRemove; ++i)
{
aNumArr.shift();
}
}
else
{
aNumArr.sort(Array.DESCENDING);
for(var i:int = 0; i > iMinsToRemove; --i)
{
aNumArr.pop();
}
}
}
Basically aNumArr:Array
is the array of integers specified.
AndiNumbersOfMinsToRemove:int
is the number of min values to remove.
The Assignment requires me to return nothing.
I know Array.NUMERIC and Array.DESCENDING would change the position of the elements, but I can't seem to figure out the logic on how to keep their positions. Please try to keep as simple a possible. I'm still an ameture.