JS---数组对象基础练习题

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/weixin_41086056/article/details/88015781

1.随机点名系统

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title></title>
    <script type="text/javascript">
        var isRun=true;
        var a = ["彭万里","高大山","谢大海","马宏宇","林莽","黄强辉","章汉夫","长江","林君雄","平山","朱希亮","李四光","甘铁生","张伍绍祖","马继祖","程孝先","宗敬先","年广嗣"];
        var a2 = new Array();
        function action(str){
            var s = document.getElementById("bt").value;
            if(s=="开始"){
                isRun=true;
                run();
                document.getElementById("bt").value="结束";
            }else{
                isRun=false;
                document.getElementById("bt").value="开始";
            }
        }
        function run(){
            var i = Math.floor(Math.random() * a.length+ 1)-1;
            document.getElementById("show").innerHTML=a[i];
            if(isRun==false){
                var b =true;
                for(var j in a2){
                    if(a2[j]==i){
                        b=false;
                    }
                }
                if(b){
                    a2[a2.length]=i;
                    return;
                }
            }
            setTimeout("run()",10);
        }
    </script>

</head>

<body>
<div style="text-align:center; margin-top:100px;width:100%;">
    <div id="show" style="margin:auto;font-size:50px;width:100px;height:100px; background:#FFEEFF"></div>
    <div style="margin-top:20px;">
        <input id="bt" type="button" onclick="action()" value="开始"/>
    </div>
</div>
</body>
</html>

2.编写函数map(arr) 把数组中的每一位数字都增加30%

<script>

    function map(arr){
        for (var i = 0; i < arr.length; i++) {
            arr[i]=arr[i]*1.3;
        }
        console.log(arr);
    }
    map([10,20,30]);
</script>

3.编写函数has(arr , 60) 判断数组中是否存在60这个元素,返回布尔类型

<script>
    function has(arr,x){
        for (var i = 0; i < arr.length; i++) {
            if (arr[i]==x) {
                return true;
            }
        }
        return false;
    }
    alert(has([21,324,2,12,6],60));
</script>

4.以下是某班级一次考试的成绩表。请计算每个学生总成绩,并按总成绩排名。统计各单科成绩第一名,输出其成绩与学号。
学号 语文 数学 英语 总成绩 备注
1 105 62 118
2 89 78 120
3 86 64 80
4 78 99 91
5 107.5 97 70
6 112 61 92
7 101 79 104
8 71 72 105
9 56 68 61
10 98 83 77

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
       table{
           border-collapse: collapse;
       }
        td,th{
            border:1px solid #000000;
            width: 100px;
            height:30px;
            text-align: center;
            line-height: 30px;
        }
    </style>
</head>
<body>
<script>
var data=[
    {StudentNumber:1,Chinese:105,Math:62,English:118,TotalScore:"",Comment:"优"},
    {StudentNumber:2,Chinese:89,Math:78,English:120,TotalScore:"",Comment:"优"},
    {StudentNumber:3,Chinese:86,Math:64,English:80,TotalScore:"",Comment:"优"},
    {StudentNumber:4,Chinese:78,Math:99,English:91,TotalScore:"",Comment:"优"},
    {StudentNumber:5,Chinese:107.5,Math:97,English:70,TotalScore:"",Comment:"优"},
    {StudentNumber:6,Chinese:112,Math:61,English:92,TotalScore:"",Comment:"优"},
    {StudentNumber:7,Chinese:101,Math:79,English:104,TotalScore:"",Comment:"优"},
    {StudentNumber:8,Chinese:71,Math:72,English:105,TotalScore:"",Comment:"优"},
    {StudentNumber:9,Chinese:56,Math:68,English:61,TotalScore:"",Comment:"优"},
    {StudentNumber:10,Chinese:98,Math:83,English:77,TotalScore:"",Comment:"优"},
];
//总成绩求和
data.map(function (t){
    t.TotalScore=t.Chinese+t.Math+t.English;
});
//总成绩排序
data.sort(function (pre,next) {
    return pre.TotalScore-next.TotalScore;
});
//各单科成绩排名
data.sort(function (pre,next) {
    return next.Chinese-pre.Chinese;
});
data.sort(function (pre,next) {
    return next.Math-pre.Math;
});
data.sort(function (pre,next) {
    return next.English-pre.English;
});
var table="<table>";
table+="<tr>";
for(var prop in data[1]){
    table+="<th>"+prop+"</th>";
}
table+="</tr>";
for(var i=0;i<data.length;i++){
    table+="<tr>";
    for(var key in data[i] ){
        table+="<td>"+data[i][key]+"</td>";
    }
        table+="</tr>";
}



document.write(table);

</script>
</body>
</html>

6.数字字母混合验证码 并验证

<input id="insert" type="text" placeholder="验证码" >
<input id="queren" type="submit" value="确认">
<script>
    /*  a--97
    *  z--122
    *
    *  A-65
    *  Z-90
    *
    * */
    var arr=[];
    //数字
    for(var i=0;i<123;i++){
        if(i<10){
            arr.push(i);
            continue;
        }
        //大写字母
        if(i<65) continue;
        if(i<91){
            arr.push(String.fromCharCode(i));
            continue;
        }
        //小写字母
        if(i<97)continue;
        arr.push(String.fromCharCode(i));
    }
    arr.sort(function () {
        return Math.random()-0.5;

    });
    arr.length=4;
    document.write(arr.join(""));
//insert=arr.innerHTML;
    var insert=document.getElementById("insert");
    var queren=document.getElementById("queren");
    queren.addEventListener("click",clickHandler);
    function clickHandler() {
        if(insert.value==arr){
            alert("正确");
        } else
        {alert("错误");}

    }

</script>
验证有点问题 求大神指导
展开阅读全文

没有更多推荐了,返回首页