JS基础:求一组数中的最大最小值,以及所在位置

         var arr = [0, 5, -3, 6, 2, -6, 10];
//定义一个最大值和一个最小值,把他们的索引值赋值给固定的两个变量
var maxValue = arr[0];
var minValue = arr[0];
var maxIndex = 0;
var minIndex = 0;
for (var i = 1; i < arr.length; i++) {
if(arr[i] > maxValue){
//把这个元素赋值给最大值,把他对应的索引值,赋值给maxIndex
maxValue = arr[i];
maxIndex = i;
}
//如果数组中的元素小于我们定义的最小值
if (arr[i] < minValue) {
minValue = arr[i];
minIndex = i;
}
}
console.log(maxValue);
console.log(maxIndex);
console.log(minValue);
console.log(minIndex);
上一篇:SQLite 分离数据库(http://www.w3cschool.cc/sqlite/sqlite-detach-database.html)


下一篇:为什么a标签中使用img后的高度多了几个像素?