WONGZiF 2022-03-03 22:47 采纳率: 100%
浏览 22
已结题

JavaScript在编写利率计算表格一按Calculate就报错。

这个是要求

img


以下是我的代码,请指出哪里需要怎么修改,感激万分

这个是HTML代码

<html>
 <title>Made By WONG_ZiF</title>
 <head><h2>Welcome to interest rate calculator</h2></head>
<body>
  
 <form name = "calculator">
  <table>
    
    <tr>
      <td>Principal(HKD) :</td>
      <td><input type="text" name="principal" value=''></td>
    </tr> 
    
    <tr>
      <td>Interest Rate(%) :</td>
      <td><input type="text" name="rate" value=''></td>
    </tr>
    
    <tr>
      <td><input type="radio" name="selectRate" value="simple">Simple Rate</td>
      <td><input type="radio" name="selectRate" value="compound">Compound Rate</td>
    </tr>
    
    <tr>
      <td>Number of Years :</td>
      <td><input type ="text" name="year" value=''></td>
    </tr>
    
    <tr>
      <td>Interest Amount(HKD) :</td>
      <td><input type="text" name="amount" value='' disabled></td>
    </tr>
    
    <tr>
      <td><input type="button" value="Calculate" onclick="document.calculator.amount.value="calculate()""></td>
      <td><input type="reset" value="Reset All"></td>
    </tr>
    
  </table>
 </form>
  
</body>
</html>

这个是JavaScript代码


function interest(principal, mode, rate, year){
    if(mode == "simple"){
        var Sl;
            Sl = principal * rate * year;
        return Sl;
      }else{ // compound interest
            var Cl;
            Cl = principal * ((1 + rate)**year - 1);
        return Cl;
      }
}

function twoDP(number) {
        var number;
        number = Math.round( num * 100 ) / 100;
        return number;
}

function calculate(){
      var P = parseFloat(document.calculator.principal.value);
      var Rt = parseFloat(document.calculator.rate.value);
      var N = parseFloat(document.calculator.year.value);
      var mode = parseFloat(document.calculator.selectRate.value);
      return twoDP(interest(P, mode, Rt, N));
}

这个是最后要呈现的界面

img

  • 写回答

3条回答 默认 最新

  • CSDN专家-showbo 2022-03-03 23:15
    关注

    修正了语法和逻辑错误,算法没看是否正确

    img

    
    <html>
    <title>Made By WONG_ZiF</title>
    <head>
        <h2>Welcome to interest rate calculator</h2>
    </head>
    <body>
    
        <form name="calculator">
            <table>
    
                <tr>
                    <td>Principal(HKD) :</td>
                    <td><input type="text" name="principal" value=''></td>
                </tr>
    
                <tr>
                    <td>Interest Rate(%) :</td>
                    <td><input type="text" name="rate" value=''></td>
                </tr>
    
                <tr>
                    <td><input type="radio" name="selectRate" value="simple">Simple Rate</td>
                    <td><input type="radio" name="selectRate" value="compound">Compound Rate</td>
                </tr>
    
                <tr>
                    <td>Number of Years :</td>
                    <td><input type="text" name="year" value=''></td>
                </tr>
    
                <tr>
                    <td>Interest Amount(HKD) :</td>
                    <td><input type="text" name="amount" value='' disabled></td>
                </tr>
    
                <tr>
                    <td><input type="button" value="Calculate" onclick="document.calculator.amount.value=calculate()"></td>
                    <td><input type="reset" value="Reset All"></td>
                </tr>
    
            </table>
        </form>
        <script>
    
            function interest(principal, mode, rate, year) {
                if (mode == "simple") {
                    var Sl;
                    Sl = principal * rate * year;
                    return Sl;
                } else { // compound interest
                    var Cl;
                    Cl = principal * ((1 + rate) ** year - 1);
                    return Cl;
                }
            }
    
            function twoDP(num) {//这里参数名称有误
                var number;
                number = Math.round(num * 100) / 100;
                return number;
            }
    
            function calculate() {
                var P = parseFloat(document.calculator.principal.value)
                var Rt = parseFloat(document.calculator.rate.value);
                var N = parseFloat(document.calculator.year.value) ;
                var mode = document.calculator.selectRate[0].checked ? 'simple' :'compound';///////////这里获取模式有问题,应该判断是否选中
                return twoDP(interest(P, mode, Rt, N));
            }
    
        </script>
    </body>
    </html>
    
    

    img

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 3月12日
  • 已采纳回答 3月4日
  • 修改了问题 3月3日
  • 修改了问题 3月3日
  • 展开全部

悬赏问题

  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持