首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >书中的一个例子"Javascript -权威指南“不起作用。

书中的一个例子"Javascript -权威指南“不起作用。
EN

Stack Overflow用户
提问于 2011-12-17 23:56:26
回答 1查看 236关注 0票数 2

我在读Javascript -权威指南。我通常试着模仿他们给出的例子,这样我才能更快地学习,这正是我学习的方法。但是我重写了这个Javascript,它似乎不起作用。每当我输入数字并点击“计算”,它就不会计算。

我已经试过在其他地方寻找答案,但什么也没找到。

代码语言:javascript
复制
<html>
<body>
<head><title>Factorials</title></head>



<form name="loandata">
    <table>
        <tr>
            <td colspan="3"><b>Enter Loan Information:</b></td>
        </tr>
        <tr>
            <td>1)</td>
            <td>Amount of the loan (any currency):</td>
            <td><input type="text" name="principal" size="12"
                        onchange="calculate();" /></td>
        </tr>
        <tr>
            <td>2)</td>
            <td>Annual percentage rate of interest:</td>
            <td><input type="text" name="interest" size="12"
                        onchange="calculate();" /></td>
        </tr>
        <tr>
            <td>3)</td>
            <td>Repayment period in years:</td>
            <td><input type="text" name="years" size="12"
                        onchange="calculate();" /></td>
        </tr>
        <tr>
            <td colspan="3">
                <input type="button" value="Compute" onclick="calculate();" />
            </td>
        </tr>
        <tr>
            <td colspan="3">
                <b>Payment Information:</b>
            </td>
        </tr>
        <tr>
            <td>4)</td>
            <td>Your monthly payment will be:</td>
            <td><input type="text" name="payment" size="12" /></td>
        </tr>
        <tr>
            <td>5)</td>
            <td>Your total payment will be:</td>
            <td><input type="text" name="total" size="12" /></td>
        </tr>
        <tr>
            <td>6)</td>
            <td>Your total interest payments will be:</td>
            <td><input type="text" name="totalinterest" size="12" /></td>
        </tr>
    </table>
</form>

<script>
function calculate() {
    // Get the user's input from the form. Assume it is all valid.
    // Convert interest from a percentage to a decimal, and convert form
    // an annual rate to a monthly rate. Convert payment period in years
    // to the number of monthly payments.
    var principal = document.loandata.principal.value;
    var interest = document.loandata.interest.value / 100 / 12;
    var payments = document.loandata.years.value * 12;

    // Now compute the monthly payment figure, using esoteric math..
    var x = Math.pow(1 + interest, payments);
    var monthly = (principal*x*interest)/(x-1);

    // Check that the result is a finite number. If so, display the results.
    if (!isNaN(monthly) &&
        (monthly != Number.POSITIVE_INFINITY) &&
        (monthly != Number.NEGATIVE_INFINITY) {

        document.loandata.payment.value = round(monthly);
        document.loandata.total.value = round(monthly * payments);
        document.loandata.totalinterest.value = round((monthly * payments) - principal);
    }
    // Otherwise, the user's input was probably invalid, so don't display anything.
    else {
        document.loandata.payment.value = "";
        document.loandata.total.value = "";
        document.loandata.totalinterest.value = "";
    }
}

// This simple method rounds a number to two decimal places.
function round(x) {
    return Math.round(x*100)/100;
}
</script>

</body>
</html>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-12-18 00:00:37

我注意到的第一件事是你遗漏了一个括号:

if (!isNaN(monthly) && (monthly != Number.POSITIVE_INFINITY) && (monthly != Number.NEGATIVE_INFINITY) *)* {

我发现JSLint帮助我处理这些语法错误。如果您正在使用IDE,则可以使用一些集成工具来使JSLint与它们一起工作。

贾里德还间接地指出了另一个网站,JSFiddle,它可以用来调试HTML+CSS+JS。

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8548838

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档