Assignment: Interest

Grab, unzip, and upload the starter code.

Part 1

Write some Javascript code for computing accumulated interest (compounded once a year). Your code should start by defining three variables: initial, the initial investment amount in dollars, rate, the annual interest rate, and N, the length of the investment in years. Using a while loop, your code should compute the investment value at the end of each year, and print the accumulated investment for each year, up to N years. Instead of using the print command, instead use console.log, which outputs text to the Javascript console in Chrome. (You’ll need to open that console. (On my Mac, it’s under the View/Developer menu selection in Chrome.)

We realize that you can compute the final interest in one step using a closed-form equation, but your code must compute the interest one year a time.

The output for each year should look something like:

year 36: $579.18

Because your total is a floating point number, it will have more than two digits to the right of the decimal point. You can truncate this to two digits using this bit of code:

Math.round(100 * total) / 100

Before you start writing code, consider the expression for the total amount in year 0: total = initial. Now consider the expression for the total at the end of year 1: total = total + total * rate. What about year two, three, etc? Extract the common expression and build your while loop around that.

Part 2

Define a new variable target, a target dollar amount. Add a condition to your while loop that terminates before N years if the total exceeds target. You should test your code by specifying different values of initial, rate, and N to make sure that your code is working properly.

Bonus (optional)

Draw a graph of your wealth by year using processing.js commands, found here. Processing is already enabled in the starter code.