Javascript兴趣计算器

时间:2013-08-23 07:22:13

标签: javascript

有一个基本的javascript兴趣计算器,我需要修改它来计算select选择的三种不同储蓄计划中的一种。

基本代码工作正常:

function CalculateNonComp() {
if (!parseFloat(document.noncomp.noncomp_principal.value)) {
    document.noncomp.noncomp_principal.value = 0;
};
if (!parseFloat(document.noncomp.noncomp_interest.value)) {
    document.noncomp.noncomp_interest.value = 1.4;
};
if (!parseFloat(document.noncomp.noncomp_length.value)) {
    document.noncomp.noncomp_length.value = 30;
};
var Principal = parseFloat(document.noncomp.noncomp_principal.value);
var DailyInterest = parseFloat(document.noncomp.noncomp_interest.value) / 1;
DailyInterest = DailyInterest / 100;
var Days = parseInt(document.noncomp.noncomp_length.value) * 1;
var Result = Principal * (1 + DailyInterest * Days);
document.getElementById('noncomp_result').innerHTML = Result.toFixed(2);
}

一旦我尝试实现if .... else if .... else语句来计算select选择的正确储蓄计划,我的问题就开始了。

我尝试过以下方法:

function CalculateNonComp() {
if (document.noncomp.plan.value = "1");
if (!parseFloat(document.noncomp.noncomp_principal.value)) {
    document.noncomp.noncomp_principal.value = 0;
};
if (!parseFloat(document.noncomp.noncomp_interest.value)) {
    document.noncomp.noncomp_interest.value = 1.4;
};
if (!parseFloat(document.noncomp.noncomp_length.value)) {
    document.noncomp.noncomp_length.value = 30;
};


var Principal = parseFloat(document.noncomp.noncomp_principal.value);
var DailyInterest = parseFloat(document.noncomp.noncomp_interest.value) / 1;
DailyInterest = DailyInterest / 100;


var Days = parseInt(document.noncomp.noncomp_length.value) * 1;

var Result = Principal * (1 + DailyInterest * Days);

document.getElementById('noncomp_result').innerHTML = Result.toFixed(2);

else if (document.noncomp.plan.value = "2");


if (!parseFloat(document.noncomp.noncomp_principal.value)) {
    document.noncomp.noncomp_principal.value = 0;
};
if (!parseFloat(document.noncomp.noncomp_interest.value)) {
    document.noncomp.noncomp_interest.value = 1.6;
};
if (!parseFloat(document.noncomp.noncomp_length.value)) {
    document.noncomp.noncomp_length.value = 60;
};


var Principal = parseFloat(document.noncomp.noncomp_principal.value);
var DailyInterest = parseFloat(document.noncomp.noncomp_interest.value) / 1;
DailyInterest = DailyInterest / 100;


var Days = parseInt(document.noncomp.noncomp_length.value) * 1;

var Result = Principal * (1 + DailyInterest * Days);

document.getElementById('noncomp_result').innerHTML = Result.toFixed(2);

else(document.noncomp.plan.value = "3");


if (!parseFloat(document.noncomp.noncomp_principal.value)) {
    document.noncomp.noncomp_principal.value = 0;
};
if (!parseFloat(document.noncomp.noncomp_interest.value)) {
    document.noncomp.noncomp_interest.value = 1.8;
};
if (!parseFloat(document.noncomp.noncomp_length.value)) {
    document.noncomp.noncomp_length.value = 90;
};


var Principal = parseFloat(document.noncomp.noncomp_principal.value);
var DailyInterest = parseFloat(document.noncomp.noncomp_interest.value) / 1;
DailyInterest = DailyInterest / 100;


var Days = parseInt(document.noncomp.noncomp_length.value) * 1;

var Result = Principal * (1 + DailyInterest * Days);

document.getElementById('noncomp_result').innerHTML = Result.toFixed(2);

}

感谢任何提示或帮助!

4 个答案:

答案 0 :(得分:1)

您需要使用“{”和“}”

来定义条件语句的范围
if(condition 1) {
code
} else if () {

}

答案 1 :(得分:1)

检查您的if else语法

if else js

答案 2 :(得分:0)

正确使用if .. else if .. else语句是:

if (condition) {
    // do something
} else if (condition) {
    // do something
} else {
    // do something
}

将此与您的代码进行比较,并考虑您做错了什么;]

答案 3 :(得分:-2)

我尝试使用java脚本计算表单和文本框验证的所有输入验证的简单兴趣,并应用css

         <html>

        <head>
       <title>SIMPLE INTEREST CALCULATION</title>

       <link type="text/css"  href="style.css"  rel="stylesheet ">

        <script type="text/javascript" src="style.js">  

        </script>

      </head>

       <body>

     <form name="frm">

    <center>

    <h1>....Simple Interest Calculation....</h1>

     <div id="div">

    <table id="t1">

     <caption> </caption>

     <tr> 
     <td>Amount Rs/-
     </td>

     <td> 

     <input type="text" name="amount" id="amount" value="" class="input" min="0"  onkeyup="AllowNumber()"/>
      </td>
     </tr>

      <tr>
      <td>Rate Rs/-
    </td>

       <td><input type="text" name="rate" id="per" value="" min="0" max="3"     class="input" />
     </td>
    </tr>

   <tr>
   <td>Time peroid:</td>
   <td>
    <input type="number" name="time" value="" id="time" min="0" class="input"/>

   <select id="peroid" class="choice">

  <option>Choose</option>

  <option value="year">Year</option>

  <option value="month">Month</option>

  <option value="weeks">Weeks</option>

   <option value="days">Days</option>

   </select>

   </td>

   </tr>

   <tr>

   <td>
Total Interest:
   </td>

    <td>
    <input type="text" id="interest" value="" name="interest" class="input" readonly/>
  </td>
  </tr>

  <tr><td>TotalAmount:</td>
    <td><input type="text" id="total" name="total" value="" class="input" readonly/></td></tr>

    <tr>
   <td><input type="button" name="cal" id="cal" value="Calculate" class="cal" onclick="cali()"/>
   </td>

     <td><input type="reset" name="clear" id="cal" value="Clear" class="cal"/>    </td></tr>
    </table>
   </center>
    </div>
  </form>
  </body>
   </html>


   **CSS Code**//style.css

  .input
   {
    font-weight:bold;
    width:260px;
    height:32px;
    font-size:1em;
    text-align: right
   }

    #amount
    {
   border-radius: 40px;
    border:2px solid teal;
    }
    #per
    {
    border-radius: 40px;
    border:2px solid teal;
     }

    #time
   {
     border-radius: 40px;
     border:2px solid teal;
    width:130px;
    height:30px;
     }

     #interest
    {
     border-radius: 40px;
     border:2px solid teal;
    }
     #total
    {
    border-radius: 40px;
    border:2px solid teal;
   }
   .cal
    {
    font-weight:bold;
   width:250px;
   height:40px;
   font-size:1em;
    border-radius: 50px;
   border:2px solid black;
   }
   #t1
    {
     color:teal;
    font-size:2em;
    border-radius: 40px;
    font-weight:bold;

    font-family:Gabriola;
    }

    select
   {
   border-radius: 40px;
   border:2px solid teal;
   width:130px;
   height:35px;
   font-size:1em;
   }

   #div
   {
    background-color: lightgrey;
   width: 600px;
    padding: 20px;
    border: 25px solid green;
     margin: 20px;
    border-style: inset;
    border-radius: 50px;
    box-shadow: 10px 10px 5px red;
    }
    h1
   {
   color:purple;
   hypens:auto;
    text-shadow: 10px 10px 10px black;
  font-weight:bold;
  font-variant: small-caps;
   font-size:3em;
  }
  #d1
 {
    background-color: lightgrey;
   width: 850px;
   padding: 25px;
    border: 25px solid navy;
    margin:60px;
    margn-left:5px;
   }

  #cal
  {
  background-color:teal;
  width:280px;!important
  height:33px;!important
  }

  **JavaScript Code**//style.js

    //Empty text box validation

 function cali()
 {
   var p,r,n,y,m,w,d;
  if(document.frm.amount.value == "" )
   {
   alert("PLEASE ENTER PRINCIPLE AMOUNT");
    document.frm.amount.alert();
   }
   else if(document.frm.rate.value=="")
  {
   alert("ENTER RATE OF INTEREST");
   document.frm.rate.blur();
   }

   if(parseFloat(per.value)>100)//text box allow only range of within 100
   {
    alert("Please Enter The Interest Rate Less Then 100");
     per[i].focus();
    return (false);
  }

   if(parseFloat(per.value)<0)//text box allow only range of Greater Than Zero
   {
     alert("Please Enter The Interest Rate Greater then 0");
     per[i].focus();
     return (false);
   }

     if(!document.getElementById("per").value.match(/^[0-9]*\.?[0-9]*$/))//text box allow only numbers and allow only one dot
    {
  alert("Enter Correct Interest Rate");
   return (false);
   }
   else if(document.frm.time.value=="")
   {
    alert("ENTER NUMBER OF TIME PERIOD");
    document.frm.time.prompt();
   }
   else
  {
  p=parseInt(document.frm.amount.value);

  r=parseFloat(document.frm.rate.value);

  n=parseInt(document.frm.time.value);

   peroid=document.getElementById("peroid").value;

   if(peroid=="year")// per year calculation
   {
    y=(p*r*n)/100;
    document.frm.interest.value="Rs :" +Math.ceil(y);
  }

   else if(peroid=="month")//per month calculation
  {
   m=(p*r*(n/12))/100;
  document.frm.interest.value="Rs :" +Math.ceil(m);
  }

   else if(peroid=="weeks")//per week calculation
    {
  var w=(p*r*(n/52))/100;
   document.frm.interest.value="Rs :" + Math.ceil(w);
   }


    else if(peroid=="days")//per days calculation
   {
   d=(p*r*(n/365))/100;
   document.frm.interest.value="Rs :" + Math.ceil(d);
  }

    document.frm.total.value="Rs:"+Math.ceil((p+m)||(p+y)||(p+w)||(p+d));
   }
   }

   //Text Box Allow Only The Numeric Values not allow single dot or char

   function AllowNumber(){
   if (!frm.amount.value.match(/[0-9]+$/) && frm.amount.value !="")
    {
       frm.amount.value="";
     frm.amount.focus();
    alert("Please Enter only Numers In This Field");
    }
    }