-6

I am trying to get the minimum value from an array without changing the way the the value are aligned.

function removeSmallest(numbers){
var min=Math.min.apply(null,numbers);
var indexvalue=numbers.indexOf(min);
delete numbers[indexvalue];
return numbers;
}
alex472
  • 25
  • 1
  • 10
  • 4
    Welcome to Stack Overflow! Please take the [tour], have a look around, and read through the [help], in particular [*How do I ask a good question?*](/help/how-to-ask) – T.J. Crowder Feb 28 '17 at 19:33
  • *Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers.* – BSMP Feb 28 '17 at 19:36

1 Answers1

1

Two issues:

  1. Math.min.apply(null, numbers) should be Math.min.apply(Math, numbers) (note Math instead of null)

  2. Normally, delete is not the right tool for arrays (see this question's answers for details; use splice to remove array entries if you're going to modify the array in place:

    numbers.splice(indexvalue, 1);
    

You might also consider creating and returning a new array instead of mutating and returning the same array you received.

Community
  • 1
  • 1
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875