function removeSmallest(numbers) {
// First step is to remove the smallest value given array
// Filter the smallest number out with Math.min.apply
let smallestNumber = Math.min.apply(Math,numbers);
// Now remove the number from the array
// Find the index of smallest number
let indexOfSmallestNumber = numbers.indexOf(smallestNumber);
// Remove the smallest number with splice method
numbers.splice(indexOfSmallestNumber,1);
return numbers;
}
This challenge asked to remove the smallest value given in an array of numbers. The first step was to find the smallest number in the array which was achieved by using the Math.min function which defined by MDN is:
"The Math.min() function returns the smallest of zero or more numbers."
But because the Math.min()
function only takes number values, the .apply()
had to be used to call the action on the given numbers array. Hence, Math.min.apply(Math, numbers)
.
After finding the smallest number the next step was to remove that number from the existing numbers array. This was done using the "splice method" .
The splice method alters an array by removing or adding elements given index numbers. Using the .indexOf()
method to find the index of the smallest number was the first step. After finding the index number, .splice()
was then used to remove the mentioned number and afterwards the original numbers
array was returned.
Thanks for your detailed explanation.