<!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>数字循环</title>
</head>
<body>
把12345变成 12345432123454321....这样的数
[1, 2, 3, 4, 5, 4, 3, 2, 1, 2]
<script>
const arr = [];
const arr2 = [1, 2, 3, 4, 5];
let len = arr2.length;
const N = 10
let flag = true
let sub = 0
for(let i=0; i < N; i++) {
if(flag) {
sub = sub > 0 ? sub : i > len ? 1 : 0
arr.push(arr2[sub++])
if(sub === len) {
flag = false
sub = len - 2
}
} else {
arr.push(arr2[sub--])
if(sub === -1) {
flag = true
}
}
}
console.log(arr);
</script>
</body>
</html>
网友评论