You have a typo in your code, the array has no lenths property
function removeSmallest(numbers) { var smallestNumberKEY = 0; for (var i = 0; i < numbers.length - 1; i++) { if (numbers[i + 1] < numbers[i]) { smallestNumberKEY = i + 1; numbers.splice(smallestNumberKEY, 1); } } return numbers; } document.write(removeSmallest([2, 1, 3, 4, 5, 1]));
But your algorithm will not work for another array, for example [5, 3, 1, 4, 1] , it will also remove the value 3 .
You can find the min value using the Math.min function and then filter the array
function removeSmallest(arr) { var min = Math.min(...arr); return arr.filter(e => e != min); }
source share