【算法】数组反向算法
<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">
<title>Document</title>

</head>

<body>

<script>
    var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9];
    var newArr = [];
    for (var i = 0; i < arr.length; i++) {
        newArr[i] = arr[arr.length - 1 - i];
    }
    console.log(newArr);
</script>

</body>

</html>

newArr[i] = arr[arr.length - 1 - i];
arr.length-1-i,最大长度减1减i得出最后的数组依次赋值给newArr[i]

发表评论