
[RESOLVED] Math formula problems. PLS HELP...
Hi! Need help here.
I'm making a loan processing system using javascript. But I can't get through with the right outputs because I cant make one formula right. Here are the specifications and requirements:
Input:
name of borrower
loan amount
interest rate (% per annum)
term of loan (years)
Output:
Name of borrower
Loan amount
Interest rate
Term
Rebate (1% if years/term is less than 5 years)
Amount due (future value only... or if term is less than 5 years, F minus rebate)
variables:
P=present value (loan amount)
r=interest rate
n=term of loan in years
F=future value (amount due after term)
Solutions:
F= P(1+r)^n
rebate= Future value * 0.01
Amount due= if n>=5, then F, else F*0.99
Code:
<script type="text/javascript">
//x=prompt("Enter name of borrower: ");
//alert("Name of borrower: "+x);
name=prompt("Enter name of borrower: "," ");
P=prompt("Enter loan amount: "," ");
r=prompt("Enter interest rate (% per annum): ", " ");
n=prompt("Enter term of loan (year): ", " ");
F=P*Math.pow(1+r, n);
AmtDue=(n>=5)? F: F*0.99;
document.write("Name of borrower: " +name);
document.write("<br> Loan amount: PhP" +P);
document.write("<br> Interest rate: " +r +'% per annum');
document.write("<br> Term: " +n +' years <hr>');
document.write("<br> Rebate: PhP " +rebate);
document.write("<br> Amount due: PhP " +AmtDue);
</script>
I'm having problems with the solution for F(Future value)... I also used Math.pow for the exponent of 1+r which is n(term or year).When I test it with these values, it displays the wrong Rebate and Amount Due:
Test Values:
loan amount(P)= 100
interest rate(r)= 5
term of loan (n)= 2
Correct Results/outputs:
Loan amount: 100
Interest rate: 5
Term: 2
Rebate: 36
Amount due: 3600
Please help me with this... I couldn't get the correct output/results because of the equation or solution for Future value(F)... Please, and thank you in advance...

F=P*Math.pow(1+r, n);
F = 100 * (6,2) [ or 100 times 6^2 ]
100 * 36 = 3600, so the math seems right. What values are actually being returned??
^_^

Values read are always strings and must be converted to numeric values.
Code:
F=P*Math.pow(1+(r*1), n);
All input should be validated before use.
Q: I found this code...
A: Then find its author.

Or maybe r = parseInt(r); before the math?
^_^

I knew the math already when I started to learn javascript. Most of it is "monkey see, monkey do" from my textbook but I added the years and months option as well as a PI table later. It may be helpful.
mortgage.htm .

Wolfshade, hey! Thanks for the help...
yah, that is the correct answer. Last night, when i tested the thing with the given values, it gave me a Rebate value of more than 10,000 and an Amount Due value of more than 13,000... I have no idea where those answers came from... Now, when I tested it again, the two didn't show any values anymore... And even the words weren't there anymore. It gave me only the output of:
Loan amount: 100
Interest rate: 5
Term: 2
it, ended there... no more rebate and amount due... even the words "Rebate:" and "Amount Due:" were no longer displayed...
I tried the r= parseInt(r); u gave but it still wouldn't show up... I didn't make any revisions last night so I'm wondering why the rebate and amount due wouldn't show up this time. What do you think is wrong? Do you have any suggestions for an alternative code?
Thanks!

Hi Clueful. Thanks for dropping by...
According to what i have learned, values are always read as variables, not strings... values that are recognized as strings are those enclosed within the "" sign... Not quite sure... But based on the entire time i've been using values, if they are just typed as mere letters without a "" symbol it is read by javascript as variable to set a value or declare something. I guess so...
Anyways, I tried to do the code you gave i am having a new problem. Last night I was still testing it and it was working fine except for the calculations of the Rebate and Amount Due. But just now, when i tried to test the thing again, it didn't display the Amount Due and Rebate... including the words "Rebate:" and "Amount Due:"... So even if i tried your suggested code, I still couldn't see if it fixed the math problem.
What do you think might be wrong with it?... Do you have any suggested alternative codes aside from the code I did?...
Thanks again!... Good day!

hi wbport! Thanks for dropping by!
i think the loan processing system that i am doing works in a different way yours does. And i should not do any additional inputs or outputs than those stated in the requirements because it is a workshop given by our professor for school. I have no choice but to stick to the given input requirements.
But thanks for the help! I really appreciate it! Good day!

All input read from the user is a string until converted. If the string happens to evaluate to a number it can be used as an operand but not involving the + operator.
Are there any messages in the error console?
Q: I found this code...
A: Then find its author.


For those who might experience the same problem i did in this thread, here is the final complete and fixed code:
Code:
<html>
<body>
Loan Processing System<hr>
<script type="text/javascript">
//x=prompt("Enter name of borrower: ");
//alert("Name of borrower: "+x);
name=prompt("Enter name of borrower: "," ");
P=prompt("Enter loan amount: "," ");
r=prompt("Enter interest rate (% per annum): ", " ");
n=prompt("Enter term of loan (year): ", " ");
document.write("Name of borrower: " +name);
document.write("<br> Loan amount: PhP" +P);
document.write("<br> Interest rate: " +r +'% per annum');
document.write("<br> Term: " +n +' years <hr>');
r= parseInt(r)
F=P*Math.pow(1+(r*1), n);
rebate=F*0.01;
AmtDue=(n>=5)? F: F*0.99;
document.write("Rebate: PhP " +rebate);
document.write("<br> Amount due: PhP " +AmtDue);
</script>
</body>
</html>
Big things come in small packages

Thanks btw for the help WolfShade, clueful and wbport!
Big things come in small packages
Thread Information
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)
Tags for this Thread
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

"
"
