Question
I have a compound interest program here. I included Math.round, but my answers keep coming out with long answers of many numbers after the decimal.
I have a compound interest program here. I included Math.round, but my answers keep coming out with long answers of many numbers after the decimal. Can you tell me why Math.round isn't rounding the numbers; and if I use toFixed(2) where would I put it? My code is below.
body {
width: 80%;
margin-left: 10%;
text-align: left;
}
/* Input:
*An amount to invest: balance
*The annual rate of growth: annualRate
*The number of months to invest: numMonths
* Processing: Use a loop to compute the balance of an account after
*a bank has paid interest for numMonths number of months.
* Output: The balance after numMonths have passed.
*/
function futureValue() {
let balance = parseFloat(document.getElementById('balance').value);
let annualRate = parseFloat(document.getElementById('annualRate').value);
let numMonths = parseInt(document.getElementById('months').value);
let monthlyRate = annualRate / 12;
// For each month, compute the interest and add it to the balance.
for (let month = 1;month <= numMonths;month++) {
// Compute the interest for a month.
let interest = balance * monthlyRate;
// Round the interest to pennies.
interest = Math.round(interest * 100) / 100;
// Add the interest to the balance.
balance += interest;
}
// Display the resulting balance for the user to see.
document.getElementById('output').innerHTML =
'Your balance after ' + numMonths + ' months will be $' + balance;
}
Balance:
Annual growth rate:
Number of months:
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started