JavaScript习题


今天总结一些简单常见js编程的练习题,熟练前面学习的js知识点。

返回最大值

<script>
    function Math() {
        this.max = function () {
            let res = -Infinity;
            if (arguments.length > 0 && arguments[0].length > 0) {
                for (let k = 0; k < arguments[0].length; k++) {
                    if (arguments[0][k] > res)
                        res = arguments[0][k];
                }
            }
            return res;
        }
    }
    var arr = [];
    for (let k = 0; k < 3; k++) {
        arr[k] = parseInt(prompt("输入一个整数", 0));
    }
    alert("最大值为:" + (new Math()).max(arr));
</script>

101-200之间有多少个素数,并输出所有素数

    <script>
        var tt=/^\d*$/;
        function ff() {
            let ss = "101到200之间的素数有:";
            for (let kk = 101; kk <= 200; kk++) {
                if (dd(kk)) {
                    ss += "<b>" + kk + "</b>,";
                }
            }
            if (ss.endsWith(","))
                ss = ss.substring(0, ss.length - 1);
            document.getElementById("div1").innerHTML = ss;
        }
        function dd(num) {
            let res = true;
            if(!tt.test(num))
                return false;
            for (let k = 2; k < num / 2; k++) {
                if (num % k == 0) {
                    res = false;
                    break;
                }
            }
            return res;
        }
    </script>


<body>

    <button onclick="ff()">输出素数</button>
    <div id="div1"></div>
</body>

求s=a+aa+aaa+aaaa+aa…a的值,其中a是一个数字。

<script>
    function ff() {
        let aa = document.getElementById("aa").value;
        let nn = document.getElementById("bb").value;
        aa = parseInt(aa);
        let res = dd(aa, nn);
        document.getElementById("span1").innerHTML = res;
    }
    function dd(aa, nn) {
        let res = 0;
        let bb = "";
        for (let k = 0; k < nn; k++) {
            bb += aa;
            let cc = parseInt(bb);
            res += cc;
        }
        return res;
    }

    function aa1() {
        let aa = document.getElementById("aa").value;
        let reg = /^[1-9]{1}$/;
        let ccc = reg.test(aa);
        if (!ccc) {
            document.getElementById("aaMsg").innerHTML = "输入数据不合法!";
        } else {
            document.getElementById("aaMsg").innerHTML = "";
        }
    }
    function bb1() {
        let aa = document.getElementById("bb").value;
        let reg = /^[1-9]\d*$/;
        let ccc = reg.test(aa);
        if (!ccc) {
            document.getElementById("bbMsg").innerHTML = "输入数据不合法!";
        } else {
            document.getElementById("bbMsg").innerHTML = "";
        }
    }
</script>


<body>
   
    <table>
        <tr>
            <td>a的值为:</td>
            <td><input id="aa" onblur="aa1()" maxlength="1" /><span class="error" id="aaMsg"></span></td>
        </tr>
        <tr>
            <td>次数:</td>
            <td><input id="bb" onblur="bb1()" /><span class="error" id="bbMsg"></span></td>
        </tr>
        <tr>
            <td>计算结果为:</td>
            <td><span id="span1"></span></td>
        </tr>
        <tr>
            <td colspan="2" align="center">
                <input type="button" onclick="ff()" value="计算" />
            </td>
        </tr>
    </table>
</body>

检验并提交

    <script>
        function ff(){
            let ss=document.getElementById("num1").value;
            let bb=false;
            let reg=/^\d*$/;
            bb=reg.test(ss);
            if(!bb){
                document.getElementById("error").innerHTML="<span class='error'>数据格式不正确!</span>";
            }else{
                let cc='';
                for(let k=0;k<ss.length;k++){
                    cc+=ss.charAt(ss.length-1-k);
                }
                document.getElementById("error").innerHTML="输入数据为:"+cc;
            }
        }
        function dd(){
            document.getElementById("error").innerHTML='';
            document.getElementById("num1").value='';
        }
    </script>

<body>
    <input id="num1" onfocus="dd()"/><span id="error"></span><br/>
    <input type="button" onclick="ff()" value="提交">
    <style>
        .error{
            color:red;
        }
    </style>
</body>

判断素数

    <script>
        function ff(){            
            var ss=document.getElementById("num").value;
            let dd=/^\d*$/;
            let bb=dd.test(ss);
            if(bb){
                ss=parseInt(ss);
                bb=zhiShu(ss);
                if(bb){
                    document.getElementById("res").innerHTML=ss+"是质数";
                }else{
                    document.getElementById("res").innerHTML=ss+"不是质数";
                }
            }else{
                document.getElementById("res").innerHTML="<font color=red>输入数据不正确</font>";
            }
        }
        function zhiShu(num){
            let res=true;
            for(let k=2;k<num/2;k++){
                if(num%k==0){
                    res=false;
                    break;
                }
            }
            return res;
        }
    </script>

<body>
    <input id="num">
    <button onclick="ff()">提交数据</button><br/>
    <div id="res"></div>
</body>

点击按钮新增下拉选择的选项

<!DOCTYPE html>

    <script>
        function ff(){
            let sel1=document.getElementById("sel");
           var s1=document.createElement("option");
           s1.value="111";
           s1.text="河南";
           sel1.options[sel1.options.length]=s1;
        }
    </script>

<body>
    <p></p>
    <select id="sel">
        <option value="">请选择</option>
        <option value="111"></option>
        <option value="222"></option>
    </select>
    <button onclick="ff()">新增</button>
</body>
</html>

关联下拉选择

<body onload="pp()">
    <script>
        //[]表示数组,{}表示对象  JSON
        var aa = [{
            id: 1, title: '西省', children: [
                { id: 11, title: '西市' }, { id: 12, title: '榆市' }, { id: 13, title: '延市' }
            ]
        }, {
            id: 2, title: '山省', children: [
                { id: 21, title: '运市' }, { id: 22, title: '临市' }
            ]
        }, {
            id: 3, title: '河省', children: [
                { id: 31, title: '洛市' }
            ]
        }];
        function pp() {
            let sel = document.getElementById("prov").options;
            for (let k = 0; k < aa.length; k++) {
                let id = aa[k].id;
                let title = aa[k].title;
                let op = document.createElement("option");  //创建option元素对象,类似<option></option>
                op.value = id;  //设置option的提交值
                op.text = title; //设置option的显示值
                sel[sel.length] = op;  //将新创建的option加入到select的options数组中
            }
        }
        function ss(){
            let sel = document.getElementById("prov").value;
            let arr=[];
            for(let k=0;k<aa.length;k++){
                let id=aa[k].id;
                if(sel==id){
                    arr=aa[k].children;
                    break;
                }
            }
            let vv=document.getElementById("city").options;
            vv.length=1;  //将数组长度设置为1,则自动去除第一个元素之后的所有元素
            for(let k=0;k<arr.length;k++){
                let op=document.createElement("option");
                op.value=arr[k].id;
                op.text=arr[k].title;
                vv[vv.length]=op;
            }
        }
    </script>
    <select id="prov" onchange="ss()">
        <option value="">请选择省</option>
    </select>
    <select id="city">
        <option value="">请选择城</option>
    </select>
</body>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值