Help....Javascript Program?
-
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.
-
Answer:
function calculate_amount(start_amount, interest_rate, years_passed) { var end_amount = start_amount * Math.pow(1.0 + interest_rate, years_passed); return Math.round(end_amount * 100)/100; } function print_amounts(start_amount, interest_rate, number_of_years) { for(var i = 1; i <= number_of_years; i++) { document.write("Year "+i+") "+ calculate_amount(start_amount, interest_rate, i)+"\n"); } } //an example with the initial amount being £1000 print_amounts(1000, 0.05, 10);
Yahoo! Answers Visit the source
Other answers
here we go for( var x=1, p=1000, r=.05; x<10; x+=1 ){ var v = (function(){return p * Math.pow( (r+1), x );})() ,f = ( function(z){ var xx = (Math.round(z*100)/100).toString(); return xx.replace( /^([0-9]{1,})(\.+[0-9]{0,})?$/gi, function(a,b,c){switch(c.length){ case 0: z = c + ".00"; break; case 1: z = c + "00"; break; case 2: z = c + "0"; break; default: z = c.substring(0,3); break; } return "$" + b + z; } ); })(v) document.write( "after year " + x + ": " + f + "<br>" ); }
Related Q & A:
- Can someone help me find a study abroad program?Best solution by Yahoo! Answers
- Is there a free service or program in Vancouver offers people with disabilities help with dating?Best solution by Yahoo! Answers
- Can someone help program this for me?Best solution by Yahoo! Answers
- I want to download full encyclopedia program, please help.Best solution by Yahoo! Answers
- Need help debugging a javascript?Best solution by allwebdevhelp.com
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.