|
-
Oct 30th, 2002, 06:42 AM
#1
Thread Starter
New Member
Simple Interest and Compound
Can anyone please help me, I have been asked to write a code for the following. The problem;
As part of a development team, you have been asked to develop small part of an e-commerce system for a company that offers mortgages and other loans on line. The task is to write a function that can calculate the monthly payment on a home mortgage or other loan, given the amount of the loan, the interest rate, the repayment period, and whether the interest is to be simple or compound.
The system should accept the following as input
Name of customer, Address, D.O.B, Occupation, The amount of loan, and how long the loan is for (months).
The base interest rate is 0.088 however, if the loan exceeds £10,000 then subtract 2.0% from the interst rate. If the length of the loan exceeds 36 months, then add 2.2% to the base interest.
the system should print out the following as output;
Name of customer, Address, D.O.B, the amount of the loan, the monthly payment on the loan (simple and compound), how long the loan is for, the total of all payments, the total interest to be paid.
INTEREST RATE FORMULA:
simple interest
let the annual rate of interest be i (as a fraction 100i percent for example percent will be 0.088) the amount of the principal be P, the number yeras be N, and the amount after years be A.
A=P(1+ni)
Compound Interest:
Let the annual rate of interest be i (as a fraction, that is 100i precent) the amount of the principal be P, the number of years be n, the number of times per year that the interest is compound be q, and the amount n years be A.
A=P(1+(i/q))nq
this is my attempt can anyone tell me where I'm going wrong please?
<html>
<head>
<title> Headwreaker</title>
<script type = "text/javascript">
<!--
var Name, // first string entered by user
Address, // second string entered by user
DOB, // Date of Birth
Occupation, // job
p, //Amount entered by user
n, //How long the loan is for in years
i, // interest
fixedInterest,
amount,
years,
firstno,
secno,
// read in name from user as a string
Name =
window.prompt( "Enter name", "" );
// read in address from user as a string
Address =
window.prompt( "Enter address", "" );
// read in DOB from user as a string
DOB =
window.prompt( "Enter DOB", "" );
// read in Occupation from user as a string
Occupation =
window.prompt( "Enter Occupation", "" );
// read in Amount from user as a string
p=
window.prompt( "Enter the amount of loan ", "0" );
// read in Period from user as a string
n=
window.prompt( "Enter the loan in years", "0" );
// convert numbers from strings to integers
p=parseFloat(firstno);
n=parseFloat(secno);
// Simple intrest
// Formula i = interest P amount n No of years
//A= amount after No of years
if p=>10000;{
sum=p*(1+n*0.68));
else
sum =p*(1+n*i));
}
if n =>36;{
sum =p*(1+n*0.11));
else
sum = eval(p*(1+n*i));
}
//function to compound Fixed interest
//function Interest()
var i=0.088;
var fixedInterest=P*(Math.pow((1+(i/12)),(n*12));
document.write("Name",+ name);
document.write("Address",+ address);
document.write("D.O.B",+ d.o.b);
document.write("Amount",+ amount);
document.write("n",+ (n/12));
document.write("n",+ n);
document.write("sum",+ sum);
document.write("Amount",+ (sum-amount));
</script>
</head>
<body>
<p>Click Refresh (or Reload) to run the script again</p>
</body>
</html>
A=P(1+ni)
-
Oct 30th, 2002, 07:42 PM
#2
Stuck in the 80s
You have a lot of syntax problems you need to work out...
Your IF statements are a big one.
You have stuff like:
Code:
if p=>10000;{
sum=p*(1+n*0.68));
else
sum =p*(1+n*i));
}
Which needs to be something like:
Code:
if (p=>10000) {
sum=p*(1+n*0.68));
} else {
sum =p*(1+n*i));
}
And also you need to make sure that your parenthesis match up, like in this line:
Notice you have one more ) than (.
Also the < or > always come before the = in >= and <=. You have it the other way around.
And finally, variables are case sensitive. So when you declare p, make sure you use p in the code, not P:
Code:
var fixedInterest=P*(Math.pow((1+(i/12)),(n*12));
//also, check variable names in this block:
document.write("Name",+ name);
document.write("Address",+ address);
document.write("D.O.B",+ d.o.b);
document.write("Amount",+ amount);
document.write("n",+ (n/12));
document.write("n",+ n);
document.write("sum",+ sum);
document.write("Amount",+ (sum-amount));
Also, in the line above, you're ()'s are messed up again. I suggest spacing some of it out to see what goes with what.
Change all that and it should work fine. Remember, variables are case sensitive. There's a lot of discrepency in your code.
Last edited by The Hobo; Oct 30th, 2002 at 07:48 PM.
-
Oct 30th, 2002, 07:47 PM
#3
Stuck in the 80s
Try to fix all those things, and if you still need help, let me know.
-
Oct 31st, 2002, 04:09 PM
#4
Thread Starter
New Member
thanx
Thanx Hobo but I scrapped that code and done it again by checking what you said most of it worked fine except for the compound interest but thanx anyway for taking the time to look at it, it was much appericated. (Very new to javascript, and by the looks of it not good at it either)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|