<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script src="./遍历求最值.js"></script>
</body>
</html>
遍历求最值.js:
const array = [2, 1, 33, 22, 56, 43, 44];
let max = array[0];
for (const num of array) {
if (num > max){
max = num;//更新变量
}
}
console.log("最大值为:" ,max);
运行结果