js方面的习题

1. 写一个函数,能返回传入的任意两个数字的最大值。
2. 写一个函数,能返回传入的任意三个数字的最大值。
3. 写一个函数,能判断传入的一个数值是否是质数,如果是质数返回true,如果不是质数返回false,(质数:从2开始只能被1和自身整数的数)
4. 写一个函数,能翻转传入的任意数组,并把翻转结果通过返回值返回。(要求自己实现,不能调用数组方法)
5. 写一个函数,能对所有传入的数组按照从小到大进行排序,并把排序结果通过返回值返回
6. 利用递归函数完成斐波那契数列

由于 已经完成,先放成果图。

在这里插入图片描述

代码如下

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <div>
        请输入你的成绩:
     <input type="text" name="socore" id="a" style="width: 100px;height: 20px;">
     <button id="btn" onclick="jungle()">提交</button>
    </div>
    <div>
        请输入俩个数:
        <input type="text" name="" id="b" value="" style="width: 50px;height: 20px;">
        <input type="text" name="" id="c" value="" style="width: 50px;height: 20px;">
        <button id="btn" onclick="compare()">比较</button>
    </div>
    <div>
        请输入三个数:
        <input type="text" name="" value="" id="d" style="width: 30px;height: 30px;">
        <input type="text" name="" value="" id="e" style="width: 30px;height: 30px;">
        <input type="text" name="" value="" id="f" style="width: 30px;height: 30px;">
        <button id="btn" onclick="compareT()">比较</button>
    </div>
    <div>
        <input type="text" name="" value="" id="g" style="width: 100px;height: 20px;">
        <button id="btn" onclick="zhishu()">判断</button>
    </div>
    <div>
        <input type="text" name="" value="" id="h" style="width: 100px;height: 20px;">
        <button id="btn" onclick="sort()">开始倒置</button>
    </div>
    <div>
        <input type="text" id="i" style="width: 100px;height: 20px;">
        <button onclick="sortArray()">开始排序</button>
    </div>
    <div>
        请输入数列的长度:
        <input type="text" id="j" style="width: 20px;height: 20px;">
        <button onclick="sortF()">开始创建斐波那契数列〉〉〉</button>
    </div>
     <script type="text/javascript">
         function jungle(){
             var a=document.getElementById("a").value;
             if(a>=0&&a<=59){
                alert("不及格!")
             }else if(a>=60&&a<=79){
                 alert("及格!")
             }else if(a>=80&&a<=89){
                 alert("良好!")
             }else if(a>=90&&a<=99){
                 alert("优秀!")
             }else if(a==100){
                 alert("完美!")
             }else{
                 alert("请输入正确的值!")
             }
            // switch (document.getElementById("a").value) {
            //             case 60:
            //                 alert("及格");
            //                 break;
                    
            //             default:
            //                 alert("未识别!")
            //                 break;
            //         }
         }
         /*
    分别使用if 和 switch语句来实现一下需求
    根据成绩返回对应等级
    0-59 不及格
    60-79 几个
    80-89 良好
    90-99 优秀
    100  完美
    */
    /*
    1. 写一个函数,能返回传入的任意两个数字的最大值。
    2. 写一个函数,能返回传入的任意三个数字的最大值。
    3. 写一个函数,能判断传入的一个数值是否是质数,如果是质数返回true,如果不是质数返回false,(质数:从2开始只能被1和自身整数的数)
    4. 写一个函数,能翻转传入的任意数组,并把翻转结果通过返回值返回。(要求自己实现,不能调用数组方法)
    5. 写一个函数,能对所有传入的数组按照从小到大进行排序,并把排序结果通过返回值返回
    6. 利用递归函数完成斐波那契额数列
    */
   function compare(){
        var a=document.getElementById("b").value;
        var b=document.getElementById("c").value;
        if(a>b){
           alert(a);
       }else{
           alert(b);
       }
   }
   function compareT(){
       var a=document.getElementById("d").value;
       var b=document.getElementById("e").value;
       var c=document.getElementById("f").value;
       console.log(a);
       console.log(b);
       console.log(c);
       if(a<b){
           a=b;
       }
       if(a<c){
           a=c;
       }
       
       alert(a);
   }
   function zhishu(){
       var a=document.getElementById("g").value;
       var flag=true;
       for(var i=2;i<a;i++){
            if(a%i==0){
               alert("该数不是质数!");
               flag=false;
                break;
            }
       }
       if(flag){
           alert("该数是质数!")
       }
   }
   function sort(){
        var a=document.getElementById("h").value;
        var b=a.split(" ");
        var c=new Array(b.length);
        for(var i=0;i<b.length;i++){
            c[i]=parseInt(b[i]);
        }
        var d=new Array(c.length);
        for(var i=0;i<c.length;i++){
            d[i]=c[c.length-i-1];
        }
        alert(d);
   }
   function sortArray(){
        var a=document.getElementById("i").value;
        var b=a.split(" ");
        var c=new Array(b.length);
        for(var i=0;i<b.length;i++){
            c[i]=parseInt(b[i]);
        }
        for(var i=0;i<c.length-1;i++){
            for(var j=0;j<c.length-i-1;j++){
                if(c[j]>c[j+1]){
                    var t=c[j];
                    c[j]=c[j+1];
                    c[j+1]=t;
                }
            }
        }
        alert(c);
   }
   function sortF(){
       var n=document.getElementById("j").value;
       var array=new Array(n);
       function s(n){
           if(n==3){
               return 3;
           }else if(n==2){
               return 2;
           }else if(n==1){
               return 1;
           }else{
               return s(n-1)+s(n-2);
           }
       }
       for(var i=0;i<n;i++){
           array[i]=s(i+1);
       }
        alert(array);
   }
     </script>
</body>
</html>
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值