数字加减乘除

1 篇文章 0 订阅

<script type="text/javascript">

        onerror = unhandled;
        function unhandled(msg, url, line) {
            alert('There has been an unhandled exception.');
        }

        function add() {
            alert($("#int1").val() + $("#int2").val());
        }

        function divide() {
            if (isNaN($("#int1").val()) || isNaN($("#int2").val())) {
                throw ('one or more values are non-numberic');
            }
            alert($("#int1").val() / $("#int2").val());
        }
    </script>

Integer 1: <input type="text" id="int1" /> <br />
    Integer 2: <input type="text" id="int2" /> <br /><br />
    <input type="button" name="add" value="Add" οnclick="add()" />
    <input type="button" name="divide" value="Divide" οnclick="divide()" />

Question 1: if the user enters the number 2 in the int1 textbox, the number 3 in the int2 textbox, what does the webpage displays?

Question 2: If the user enters the number 2 in the int1 textbox, the number 0 in the int2 textbox, what does the webpage displays?

Question 3: If the user enters the number 2 in the int1 textbox, the letter a in the int2 textbox, what does the webpage displays?


Answer: 

Q1,  23. 原因:js中得到的值都是string类型的,所以直接相加会得到 23,要想得到正确的值,需要改成 alert(parseInt($("#int1).val()) + parseInt($("#int2).val()));

Q2, infinity, 原因,在js中 除以0,得到是 infinity,即无穷大,而不会报异常。

Q3, There has been an unhandled exception. 原因,a不是数字,会throw('one or more values are non-numberic'); 

然后,就调用unhandled函数,抛出one or more values are non-numberic


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值