Please I’m trying to find minimum value inside my array and replace the minimum value with X for the same element not to be picked as minimum over again
Here is what I did
Var arr = [3,8,2,5,1]; Var minIndex;
While(arr.indexOf(‘X’) === -1){
minIndex = 0;
$.each(arr,function(index,value){
if (value < arr.[minIndex]){
minIndex = index;
}
});
arr[minIndex] = ‘X’;
}
Console.log(arr);
But only the first minimum which is 1 and is replaced with X
I don’t know what I’m doing wrong
Any help please
Replacing element of array with X
2
Answers
You could use
Array::reduce()
. As mentioned by the author he wants to skip the minimum value for the next iteration, I guess we could remember the found minimum index:If you really do not care about the array. Sort the array and remove them as you use them. There is no need to keep looking up the min.
using a generator