I am trying to solve a problem which states to remove(delete) the smallest number in an array without the order of the elements to the left of the smallest element getting changed . My code is -:
function removeSmallest(numbers){
var x = Math.min.apply(null,numbers);
var y = numbers.indexOf(x);
numbers.splice(y,1);
return numbers;
}
It is strictly given in the instructions not to mutate the original array/list. But I am getting an error stating that you have mutated original array/list . How do I remove the error?
splicewill mutate an array, I think this is why your solution is being rejected. In order to avoid mutating the original array, you need to express the solution as building up a new array from the original one, sans smallest element.nullorNaN? This would retain the original order and positions of the remaining values.