
Another one....why me?
This one kills me, it involves javascript and math, my two worst enemies!!!
If somebody knows how this works...I will worship you forever!
A person invests $1,000 in a saving account yielding 5% interest. Assuming that all interest is left on deposit, calculate and display the amount of money in the account at the end of each year for 10 years. Use the following formula for determining these amounts:
a = p(1 + r)n
where
p is the principal (original) amount invested,
r is the annual interest rate,
n is the number of years and
a is the amount on deposit at the end of the nth year.
Hint:
JavaScript does not include an exponentiation operator. You can use the Math object's pow method for this purpose. Math.pow(x,y) calculates the value of x raised to the yth power. Method Math.pow takes two numbers as arguments and returns the result. Therefore, the statement for the formula may be:
amount = principal * Math.pow(1.0 + rate, year);
Unlike many other programming languages, JavaScript does not provide numeric formatting capabilities that allow you to precisely control the display format of a number. You can use the expression
Math.round(amount * 100)/100
to format your numbers. This expression multiplies the current value of amount by 100 to convert the value from dollars to cents, then uses the Math object's round method to round the value to the closest integer. The result is then divided by 100, to produce a dollar value that has a maximum of 2 digits to the right of the decimal point.

We don't do homework. Attempt it yourself, then if you still have problems, show us what you have done.
Originally Posted by kookers
Unlike many other programming languages, JavaScript does not provide numeric formatting capabilities that allow you to precisely control the display format of a number. You can use the expression
Math.round(amount * 100)/100
Not true use toFixed

You have to first convert the passed value into FLOAT.
Here is the example.
<script language="javascript" type="text/javascript">
function calculate() {
var principal = parseInt(document.getElementById('p').value);
var rate = parseFloat(document.getElementById('r').value);
var year = parseInt(document.getElementById('n').value);
var amount = principal * Math.pow(1.0 + rate, year);
document.getElementById('calculation').value = amount;
}
</script>
<body>
<form action="" method="get" name="form1">
Principal: <input name="p" type="text" id="p"/><BR>
Rate: <input name="r" type="text" id="r" /><BR>
Year: <input name="n" type="text" id="n" /><BR>
<input name="cal" type="button" value="calculate" onclick="calculate();" /><BR>
Cal: <input name="calculation" type="text" id="calculation" />
</form>
</body>

@Suhas: Why you doing his homework?
If you help one then the board will be flooded with this type of example.
Instead of the person getting a grade based on their own academic capability, you have just help some one cheat their way into a better position than they deserve.
So next time please think again & especially when someone has pointed out the obvious.

Originally Posted by \\.\
@Suhas: Why you doing his homework?
If you help one then the board will be flooded with this type of example.
Instead of the person getting a grade based on their own academic capability, you have just help some one cheat their way into a better position than they deserve.
So next time please think again & especially when someone has pointed out the obvious.
Dear comic book guy....or atleast thats who I picture you to be,
.......
Last edited by TheBearMay; 04292007 at 11:08 AM.
Reason: Attacking another member is not encouraged...

Originally Posted by Fang
We don't do homework. Attempt it yourself, then if you still have problems, show us what you have done. Not true use toFixed
....wow, Im touched...not true use, lol...

Originally Posted by kookers
Dear comic book guy....or atleast thats who I picture you to be...
This is a forum for helping people with issues over scripting and the site development, not duping people into doing YOUR homework.
Last edited by TheBearMay; 04292007 at 11:09 AM.
Reason: Removed attack from quote...

Doing peoples homework for them is a breach of the terms of service for using these forums and the fastest way to get yourself banned when the forum owners see that you have done so.
These forums are for people to get assistance to help them to learn how to do things themselves and the last thing that any of those who provide that assistance want is for those legitimate requests for assistance to be buried under an avalanche of DO THIS FOR ME demands. If writing some code is a part of a course that you are doing then it is obviously something you need to know for yourself in order to be able to use that course in your future work and if you don't intend using it in the future then you are DOING THE WRONG COURSE.
Thread Information
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)
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

