美文网首页
数字循环

数字循环

作者: __鹿__ | 来源:发表于2022-03-02 11:59 被阅读0次
    <!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>
    

    相关文章

      网友评论

          本文标题:数字循环

          本文链接:https://www.haomeiwen.com/subject/njbsrrtx.html