• Theory
  • Theory

Eighth program: “Money makes money”

You’re ready to write one more program for the boss. He liked saving air miles and now he wants to save money for new lines of business. Naturally, he went to a bank, found out about all the terms and conditions, and is now asking you to create a program for calculating the deposit. Here is the technical assignment:

Meow! I need to calculate how much money I will make on deposits with different terms and conditions. Write a calculateDeposit function with four parameters:

  1. Initial amount of the deposit;
  2. Annual interest rate (a number from 0 to 100);
  3. Term of deposit in months;
  4. Whether interest is capitalized monthly (a Boolean flag).

The function must return the total amount of the deposit rounded to dollars using Math.round(). Use any parameter names.

For simple interest, divide the annual rate by 100 and by 12, then multiply by the number of months and the initial deposit. Add this interest to the initial amount.

Calculating a deposit with capitalization is more difficult: every month, interest calculated from the current deposit amount is added to that amount (divide the annual interest rate by 100 and by 12), and the interest for the next month is calculated from the increased amount of the deposit.

If you are having trouble calculating interest on deposits, check out the hint.

Investing 1000 dollars at 8% per annum for 3 months.

If it’s a simple deposit, the total amount will be:

// Total interest
(0.08 / 12) * 3 * 1000 = 20 dollars
// Total deposit amount
1000 + 20 = 1020 dollars

If it’s a deposit with capitalization, the total amount will be:

The intermediate values below are approximate displays only. Keep the full values in your calculations and use Math.round only on the final amount.

// Amount after the first month
1000 + (0.08 / 12) * 1000 ≈ 1006.6667 dollars
// Amount after the second month
1006.6667 + (0.08 / 12) * 1006.6667 ≈ 1013.3778 dollars
// Amount after the third month (final amount)
1013.3778 + (0.08 / 12) * 1013.3778 ≈ 1020.1336 dollars → 1020 dollars

When you are done, press Hey boss, here’s your program!, and Muffin will come by to test it. Just don’t call Muffin for silly little things! The boss can get mad!

  • index.html
HTML
HTML

You’ve gone to a different page

Click inside the mini-browser to shift the focus onto this window.

100%
Granny Muffin

Easy there, pal!

To access the Functions challenges in the JavaScript programming, you need to sign up first.