普通方法
- 设置第1个为最小值,
idx/point
为变量 - 循环
- 如果 cur < point 值,则 point = cur
- 循环结束,即可以找出最小值
代码实现
const arr = [30, 10, 111, 35, 1899, 50, 45, 2, 1];
function min(arr) {
var idx = 0;
var point = arr[0];
for (var i = 0; i < arr.length; i++) {
if (i === idx) continue;
var cur = arr[i];
if (cur < point) {
point = cur;
}
}
return point
}
console.log(min(arr));
const arr = [30, 10, 111, 35, 1899, 50, 45, 2, 1];
function min(arr) {
var point = arr[0];
for (var i = 1; i < arr.length; i++) {
var cur = arr[i];
if (cur < point) {
point = cur;
}
}
return point
}
console.log(min(arr));
参考