美文网首页
前端的简单编程

前端的简单编程

作者: KingJX | 来源:发表于2018-08-20 23:04 被阅读0次
    <!DOCTYPE html>
    <html>
        <head>
            <meta charset="UTF-8">
            <title></title>
        </head>
        <body>
            <table border="1" id="t1">
                
            </table>
        </body>
    </html>
    <script type="text/javascript">
        //1、判断一个数是奇数还是偶数
        var num1 = 9
        if(!(num1 % 2)){
            alert(num1+'是偶数')
        }
        else{
            alert(num1+'是奇数')
        }
        
        //2、给一个年份,判断是否是闰年
        var year = 2018
        if((year%4==0 && year%100!=0)||(year%100 == 0 && year%400==0)){
            alert(year+'是闰年')
        }
        else{
            alert(year+'不是闰年')
        }
        
        //3、给一个数n,计算n的阶乘
        var num2 = 5
        var content = 1
        for (var i = 1; i<=num2; i++) {
            content *= i
        }
        alert(content)
        
        
        //4、打印99乘法表
        var m1 = 1
        var n1 = 1
        var table =[]
        var content =[]
        for (var i = 1; i <= 9; i++) {
            content = []
            for (var j = 1; j <= i; j++) {
                 content.push(j,'*',i,'=',j*i)
            }
            table.push(content.join(' '))
        }
        for (i = 0; i<table.length; i++) {
            console.log(table[i])
        }
        
        //5、计算1-1/2+1/3-1/4 …  1/100的和
        var sum = 1
        for (var bottom = 2; bottom<=100; bottom++) {
            if(bottom%2 == 0){
                sum -= (1/bottom)
            }
            else{
                sum += (1/bottom)
            }
        }
        console.log(sum)
        
        //6、给一个n,求1!+2!+3!+4!+5!...+n!
        var num3 = 5
        var sum = 0
        var content = 1
        for (var i = 1; i<=num3; i++) {
            for (var j = i; j <= i; j++) {
                content *= i
            }
            sum += content
        }
        alert(sum)
            
        //7、找到所有的水仙花数
        var a, b, c
        for (var i = 100; i <= 999; i++) {
                a = Math.floor(i/100)
                b = Math.floor((i%100)/10)
                c = i%10
                if((Math.pow(a,3)+Math.pow(b,3)+Math.pow(c,3))==i){
                    console.log(i)
                }
                
        }
            
        //8、输入三个数,找到其中最大的,用一句话写出来
        alert(Math.max(2,6,3))
        
        //9、给一个数组,找到其中最大值和最小值
        var arr1 = [1,2,5,9,8,7,4]
        var arr2 = arr1.sort()
        console.log(arr2[0], arr2[arr2.length-1])
        
        //10、用while打印十行十列表格,表格里面写1-100,并且隔行变色
        var a = 0
        var i = 1
        var str1 = '<tr>'
        function creatTable(){
            str1 += '<td>'+i+'</td>'
        }
        for (i=1; i<=100;i++) {
            creatTable()
            if(i % 10 == 0){
                str1 += '</tr>'
                if(i == 100){
                    break   
                }
                 str1 += '<tr>'
            }
        }
        document.getElementById('t1').innerHTML=str1
        console.log(str1)
        
        //11、自己实现随机  a, b 之间的整型
        var a = 10
        var b = 100
        var c
        var m = true
        while (m){
            c = Math.ceil(Math.random()*100)
            if (c >= a && c <= b){
                console.log(c)
                m = false
            }
            
        }
        
        
    </script>
    
    1.png 2.png 3.png 4.png 5.png 6.png 7.png 8.png 9.png 10.png 11.png
    //10、用while打印十行十列表格,表格里面写1-100,并且隔行变色
        var a = 0
        var i = 1
        var str1 = '<tr style="background-color: red;" >'
        function creatTable(){
            str1 += '<td>'+i+'</td>'
        }
        for (i=1; i<=100;i++) {
            creatTable()
            if(i % 10 == 0){
                str1 += '</tr>'
                
                if(i == 100){
                    break
                }
                if(i % 20 == 0){
                    str1 += '<tr style="background-color: red;">'
                }
                else{
                    str1 += '<tr style="background-color: yellow;">'
                }
            }
        }
        document.getElementById('t1').innerHTML=str1
        console.log(str1)
    
    new10.png

    相关文章

      网友评论

          本文标题:前端的简单编程

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