[js] 删除数字数组中最小的一个值
#1
function removeSmallest(numbers) { if (numbers.length === 0) { return numbers; } var min = Math.min.apply(null, numbers); numbers.splice(numbers.indexOf(min), 1); return numbers; }
#2
function removeSmallest(numbers) { if (numbers.length === 0) { return numbers; } var temp = 0; numbers.forEach(function(e, i) { if (numbers[temp] > e) { temp = i; } }); numbers.splice(temp, 1); //console.log(numbers); return numbers; }
#test
removeSmallest([1, 2, 3, 4, 5]); removeSmallest([3, 2, 3, 4, 5]); removeSmallest([5, 3, 2, 1, 4]); removeSmallest([2, 2, 1, 2, 1]); removeSmallest([]);