Tuesday, 13 October 2020

Simple Interest

 

WAP to Calculate simple interest in JavaScript.

 

<html>

<head>

<title> Interest </title>

<script type="text/javascript">

function simp()

{

            {

             year=document.getElementById('t1').value;

             amt=document.getElementById('t2').value;

             rate=document.getElementById('t3').value;

 

            //-------------Validation for year------------

 

            if(document.getElementById('t1').value =='')

            {

                        alert("You must enter year");

                        return false;

            }

            var a = document.getElementById('t1').value;

            if(isNaN(a)||a.indexOf(" ")!=-1)

           {

              alert("Invalid number in year field");

            

              return false;

           }

            if((year)<0 || (year)>5)

           {

                alert("year enter bet 0-5");

               

                return false;

           }

 

            //-----------------Validation for principal------------------

 

            if(document.getElementById('t2').value =='')

            {

                        alert("You must enter principal");

                        return false;

            }

            var b = document.getElementById('t2').value;

            if(isNaN(b)||b.indexOf(" ")!=-1)

           {

              alert("Invalid number in principal field");

            

              return false;

           }

 

            //-------------------Validation for Rate---------------------

           

           

            if(document.getElementById('t3').value =='')

            {

                        alert("You must enter rate");

                        return false;

            }

            var c = document.getElementById('t3').value;

            if(isNaN(c)||c.indexOf(" ")!=-1)

           {

              alert("Invalid number in rate field");

            

              return false;

           }

            if((rate)<0 || (rate)>10)

           {

                alert("Rate enter bet 1-10");

               

                return false;

           }

            return true;

}}

 

function calc()

            {

            //----------------Calculate interest--------------

 

            var y=document.forma.t1.value

            var prin=document.forma.t2.value

            var r=document.forma.t3.value

            var interest=parseInt(y*prin*r/100)

            document.forma.out.value=interest

 

            }

 

function comp()

            {

            //----------------Calculate Compund interest--------------

            var y=document.forma.t1.value

            var prin=parseInt(document.forma.t2.value)

            var r=document.forma.t3.value

            var interest=parseInt(y*prin*r/100)

            var ci=parseInt(interest + prin)

            document.forma.out.value=ci

            }

</script>

</head>

 

<body><center> Application to calculate interest</center>

<table align="center">

<form action="#" name="forma">

<tr>

<td>

Enter Years: </td>

<td> <input type="text" name="t1" id="t1"> </td>

</tr>

<tr>

<td> Enter Principal: </td>

<td> <input type="text" name="t2" id="t2"> </td>

</tr>

<tr>

<td>

Enter Rate: </td>

<td> <input type="text" name="t3" id="t3"> </td>

</tr>

<tr>

<td> Result: </td>

<td> <input type="text" name="out" id="out"> </td>

</tr>

<tr>

<td colspan="2">

 <input type="button" value="Simple Interest" onclick="return simp();"

ondblclick="return calc();"/>

<input type="button" value="Compund Interest" onclick="return comp();">

</td>

</tr>

 

</form>

</table>

</body>

</html>