<!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>
//变量交换
let a = 1
let b = 2;
[ a, b ] = [ b, a ]
console.log(a, b) // 2, 1
//冒泡排序
function bubbleSort(arr) {
for(let i = 0; i < arr.length; i++) {
for (let j = 0; j < arr.length - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
[ arr[j], arr[j + 1] ] = [ arr[j + 1], arr[j] ]
}
}
}
return arr
}
var arr = [6, 5, 3, 1, 8, 7, 2, 4];
console.log(bubbleSort(arr));
</script>
</body>
</html>