首页 > 代码库 > js-03课 -04 自动填充数组

js-03课 -04 自动填充数组

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
<script>
    alert(fn1(5)) //1 2 3 4 5
    function fn1(n){
       if(!isDigital(n)){
           alert("请输入正整数,thank you!")
           return;
       }
        var arr = [];
        for(var i = 1; i <= n; i++){
            arr.push(i);
        }
        return arr;

    }

    alert(fn2(6,2));// 2 3 4 5 6
    alert(fn2(1,8))// 1 2 3 4 5 6 7 8
    function fn2(num1, num2){
        if(!isDigital(num1) || !isDigital(num2)) {
            alert("请输入正整数,thank you!")
            return;
        }
        var arr = [];
        if(num1 > num2){
            for(var i = num2; i <= num1; i++){
                arr.push(i);
            }
        }else if(num1 <= num2){
            for(var i = num1; i <= num2; i++){
                arr.push(i);
            }
        }
        return arr;
    }

    //判断是否是正整数
    function isDigital(num){
        if( num <= 0 || (num+‘‘).indexOf(".") != -1) {
           return false;
        }
        return true;
    }
</script>
</body>
</html>

  

js-03课 -04 自动填充数组