2018-08-20 day06-作业

2018-08-20  本文已影响0人  我是一只菜鳥
<script>
    //1.判断一个数是奇数还是偶数
    var num1 = 101;
    if (num1 % 2){
        console.log(num1 + '是奇数');
    } else{
        console.log(num1 + '是偶数');
    }
    
    //2.给一个年份,判断是否是闰年
    var year = 200;
    if ((!(year%4) && year%100) || !(year%400)){
        console.log(year + '闰年');
    }else{
        console.log(year + '不是闰年');
    }
    
    //3.给一个数n,计算n的阶乘
    function factorial(num3){
        var sum3 = 1;
        for (var i = 1; i <= num3; i++){
            sum3 *= i;
        }
        return sum3;
    }
    var num3 = 5;
    console.log(num3 + '的阶乘为:' + factorial(num3));
    
    //4.打印99乘法表
    var str1 = '';
    for (var i = 1; i < 10; i++){
        for (var j = 1; j <= i; j++){
            str1 = str1 + (j + '*' + i + '=' + i*j + ' ');
            if (i==j){
                str1 += '\n';
            }
//          console.log('%d * %d = %d', j, i, i*j);
        }
    }
    console.log(str1);
    
    //5.计算1-1/2+1/3-1/4 …  1/100的和
    function sum5(num5){
        var num;
        if (num5 == 0){
            return 0;
        }
        if (num5 % 2){
            num = 1/num5;
        } else{
            num = -1/num5;
        }
        return num + sum5(num5-1);
    }
    console.log(sum5(100));
    
    //6.给一个n,求1!+2!+3!+4!+5!...+n!
    function factorials(num6){
        if (num6 == 0){
            return 0;
        }
        var sum6 = 1;
        for (var i = 1; i <= num6; i++){
            sum6 *= i;
        }
        return sum6 + factorials(num6-1);
    }
    console.log(factorials(4));
    
    //7.找到所有的水仙花数
    for (var i = 100; i < 1000; i++){
        var b = parseInt(i / 100);
        var s = parseInt(i / 10) % 10;
        var g = i % 10;
        if (i == b*b*b + s*s*s + g*g*g){
            console.log(i);
        }
    }
    
    //8.输入三个数,找到其中最大的,用一句话写出来
    var num1 = prompt('请输入第1个数');
    var num2 = prompt('请输入第2个数');
    var num3 = prompt('请输入第3个数');
    console.log('最大的数为:%d', Math.max(num1, num2, num3));
    
    //9.给一个数组,找到其中最大值和最小值
    var arr1 = [66, 601, 55, 2, 46, 88];
    var max1 = arr1[0];
    var min1 = arr1[0];
    for (var i in arr1){
        if (max1 < arr1[i]){
            max1 = arr1[i];
        }
        if (min1 > arr1[i]){
            min1 = arr1[i];
        }
    }
    console.log('数组中的最大值为:%d; 最小值为:%d', max1, min1);
    
    arr1.sort(function (a, b){return a < b;});
    console.log('数组中的最大值为:' + arr1[0]);
    
    arr1.sort(function (a, b){return a > b;});
    console.log('数组中的最小值为:' + arr1[0]);
    
    //11.自己实现随机  a, b 之间的整型
    function radom(n, m){
        var num = Math.random();
        return n + parseInt((m - n)*num);
    }
    console.log(radom(10, 20));
    
    
</script>


<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
    </head>
    <body>
        
        <!--10.用while打印十行十列表格,表格里面写1-100,并且隔行变色-->
        <script>
            var tab = document.createElement("table")
            tab.style.width = "500px";
            var tb = document.createElement("tbody");
            tab.appendChild(tb);
            for(var i = 1; i < 11; i++){
                var row = tb.insertRow(tb.rows.length);
                for(var j = 1; j < 11; j++){
                    var col = row.insertCell(row.cells.length);
                    col.style.width = "50px";
                    col.style.height = "30px";
                    count = (i-1)*10 + j;
                    col.innerHTML = count;
                }
                var r = parseInt(Math.random()*256);
                var g = parseInt(Math.random()*256);
                var b = parseInt(Math.random()*256);
                row.style.backgroundColor = "rgb(" + r + "," + g + "," + b + ")";
            }
            document.body.appendChild(tab);
        </script>
        
        
        <!--<script>
            document.write('<table>');
            for(var i = 1; i <= 10; i++){
                document.write('<tr>');
                for(var j = 1; j <= 10; j++){
                    r = parseInt(Math.random()*256);
                    g = parseInt(Math.random()*256);
                    b = parseInt(Math.random()*256);
                    count = (i-1)*10 + j;
                    document.write('<td style="width:50px;height:20px;background-color:rgb( %d, %d , %d );">');
                    document.write(count);
                    document.write('</td>');
//                  td.style.backgroundColor = 'rgb(' + r + ',' + g + ',' + b + ')';
                }
                document.write('</tr>');
            }
            document.write('</table>');
        </script>-->
    </body>
</html>
上一篇下一篇

猜你喜欢

热点阅读