• 新学了js的函数(明白了算法,函数封装好理解)基础练习!


    给大家分享几个学函数都要写的题:(哈哈,花了120分钟,720秒的战果!)

    1.  成绩判断    level(score)  =>  不及格, 及格, 一般 , 良好 , 优秀  (ABCDE)    
        function grade(num){
            switch(Math.floor(num/10)){
                case 0:;
                case 1:;
                case 2:;
                case 3:;
                case 4:;
                case 5:document.write(+num+"分是不及格!");break;
                case 6:document.write(+num+"分是及格!");break;
                case 7:document.write(+num+"分是一般!");break;
                case 8:document.write(+num+"分是良好!");break;
                case 9:document.write(+num+"分是优秀!");break;
                case 10:document.write(+num+"分是很优秀!");break;
            }
        }
     
        
        
        
        
    2.  年月日输入格式判断   isYearOk(year) =>{true||false},  isMonthOk(month)=>{true||false}, isDayOk(year,month,day)  =>{true||false}   
        function date(year,month,day) {
            document.write("<br>"+year+","+month+","+day+"<br>")
            if(year%1==0){
                document.write(year+"年份正确")
                if(month>=1&&month<=12&&month%1==0){
                    document.write(month+" 月份正确")
                     if(year%400==0||year%4==0&&year%100!=0){
                        if(month==2){
                            if(day>=1&&day<=29&&day%1==0){
                            document.write(day+" 日期正确")
                         }else{
                            document.write(day+" 日期错误")
                         }
                        }                    
                     }else{
                        if(month==2){
                            if(day>=1&&day<=28&&day%1==0){
                            document.write(day+" 日期正确")
                         }else{
                            document.write(day+" 日期错误")
                         }
                        } 
                     }
                     if(month==1||month==3||month==5||month==7||month==8||month==10||month==12){
                        if(day>=1&&day<=31&&day%1==0){
                            document.write(day+" 日期正确")
                         }else{
                            document.write(day+" 日期错误")
                         }
                     }
                     if(month==4||month==6||month==9||month==11){
                        if(day>=1&&day<=30&&day%1==0){
                            document.write(day+" 日期正确")
                         }else{
                            document.write(day+" 日期错误")
                         }
                     }
                }else{
                    document.write(month+" 月份错误")
                }
            }else{
                document.write(year+"年份错误")
            }
        }
     
        
        
        
    3.  年月日输入计算       getTotalDay(year,month,day) => getTotalDay(2000,12,31) => 366 
        function tian(year,month,day){
            document.write("<br>"+year+","+month+","+day+"<br>")
            if(year%400==0||year%4==0&&year%100!=0){
                switch(month){
                    case 1:document.write("是本年的第"+day+"天");break;
                    case 2:document.write("是本年的第"+(day+31)+"天");break;
                    case 3:document.write("是本年的第"+(day+60)+"天");break;
                    case 4:document.write("是本年的第"+(day+91)+"天");break;
                    case 5:document.write("是本年的第"+(day+121)+"天");break;
                    case 6:document.write("是本年的第"+(day+152)+"天");break;
                    case 7:document.write("是本年的第"+(day+182)+"天");break;
                    case 8:document.write("是本年的第"+(day+213)+"天");break;
                    case 9:document.write("是本年的第"+(day+244)+"天");break;
                    case 10:document.write("是本年的第"+(day+274)+"天");break;
                    case 11:document.write("是本年的第"+(day+305)+"天");break;
                    case 12:document.write("是本年的第"+(day+335)+"天");break;
                }
            }else{
                switch(month){
                    case 1:document.write("是本年的第"+day+"天");break;
                    case 2:document.write("是本年的第"+(day+31)+"天");break;
                    case 3:document.write("是本年的第"+(day+59)+"天");break;
                    case 4:document.write("是本年的第"+(day+90)+"天");break;
                    case 5:document.write("是本年的第"+(day+120)+"天");break;
                    case 6:document.write("是本年的第"+(day+151)+"天");break;
                    case 7:document.write("是本年的第"+(day+181)+"天");break;
                    case 8:document.write("是本年的第"+(day+212)+"天");break;
                    case 9:document.write("是本年的第"+(day+243)+"天");break;
                    case 10:document.write("是本年的第"+(day+273)+"天");break;
                    case 11:document.write("是本年的第"+(day+304)+"天");break;
                    case 12:document.write("是本年的第"+(day+334)+"天");break;
                }
            }
        }
     
        
    4.1 打印等腰三角形封装    createTri(n) = > 打印n层的对应等腰三角形   
        function dengyao(n){
            for(var i=1;i<=n;i++){
            for(var k=1;k<=n-i;k++){
                document.write("&emsp;")
            }
            for(var j=1;j<=i;j=j+0.5){
                document.write("帅");
            }
            document.write("<br>")
            }
        }    
     

     4.  打印菱形封装    createDiamond(n) =>  打印n层的对应菱形因为会直接打印到页面上 => 可以看到效果  => 不设置 return也是可以的
        function lingxing(n){
            for(var i=-n;i<=n;i++){
            for(var k=1;k<=Math.abs(i);k++){
                document.write("&emsp;")
            }
            for(var j=1;j<=n+1-Math.abs(i);j=j+0.5){
                document.write("帅");
            }
            document.write("<br>")
        }
        }
     
        
    5.一个新入职,月工资为2000元的员工,每年涨当年工资5 %,到退休20年时的月工资是多少?(原题)=> getSalaryAfter(Salary) 封装  输入工资 返回对应20年后的工资
        function gongzi(monkey){
        for(var i=1;i<=20;i++){
            money=money*1.05
        }
        money=Math.round(money);
        document.write("20年后的工资约为"+money+"<br>"); 
        }
     
        
    5.一个新入职,月工资为2000元的员工,每年涨当年工资5 %,到退休20年时的月工资是多少?(原题)=> getSalaryAfter(Salary) 封装  输入工资 返回对应20年后的工资
        function gongzi(monkey){
        for(var i=1;i<=20;i++){
            money=money*1.05
        }
        money=Math.round(money);
        document.write("20年后的工资约为"+money+"<br>"); 
        }
        gongzi(5000);

        document.write("<br>6. 山上有一口缸可以装50升水,现在有15升水。老和尚叫小和尚下山挑水,每次可以挑5升。问:小和尚要挑几次水才可以把水缸挑满?通过编程解决这个问题。<br>")
        function tiaoshui(n){
            for(var i=1;true;i++){
            if(i*5>=50-n){
                break;
            }
        }
        document.write("小和尚要跑:"+i+"次<br>")
        }
     
     
    ps:if的嵌套不要超过三个,不然要把自己搞疯!!!!!
  • 相关阅读:
    tc: Linux HTTP Outgoing Traffic Shaping (Port 80 Traffic Shaping)(转)
    Linux TC的ifb原理以及ingress流控-转
    插件+组件+空间
    Q查询条件
    django中的分页标签
    QuerySet
    url
    view
    HttpReponse
    装饰器
  • 原文地址:https://www.cnblogs.com/yujiawen/p/14014932.html
Copyright © 2020-2023  润新知