Question

Modify the compound interest program presented in my JS Control Statements and Loops slides (p. 16-18) so that it calculates and displays the amount of money in the account and the amount accumulated from the interest at the end of each year using an html form for the corresponding input and output. The input boxes can be labeled "Principal", "Rate" and "Years". The two output boxes can be labeled “Amount” and “Interest”. The form should provide also two buttons “Calculate” and “Clear” as shown below.

? oxy ?How Your Investment Gro How Your Investment Gr x How YOUR IVESTMENT GROwS Principal 2000 Rate 5 Years 10 Amount 3257.79 Interest 1257.79 Calculate Clear Click Refresh (or Reload) to run this script again.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

//Code to copy

<html>

<head>
<title>Web App for Compound Interest Calculation</title>

<script type="text/javascript">
function calculate() {
p = document.getElementById("principal").value;
r = document.getElementById("rate").value;
t = document.getElementById("years").value;
n = 1;
// A = p * [[1 + (r/n)] ^ nt]
A = (p * Math.pow((1 + (r / (n * 100))), (n * t)));

//rounding to 2 decimal places
document.getElementById('amount').value = A.toFixed(2).toString();
document.getElementById('interest').value = (A.toFixed(2) - p).toFixed(2);

}

function clear() {
document.getElementById('principal').innerHTML = " ";
document.getElementById('rate').innerHTML = " ";
document.getElementById('years').innerHTML = " ";
document.getElementById('amount').innerHTML = " ";
document.getElementById('interest').innerHTML = " ";


}

</script>
<style>
div {
display: table-row;
}

label,
input {
display: table-cell;
}

</style>
</head>

<body>
<table style="width:100%" border="1" bgcolor="#c0c0c0">
<col align="right">
<col align="left">
<tr>
<td colspan="2" align="center">
<font color="blue">HOW YOUR INVESTMENT GROWS</font>
</td>
</tr>

<tr>
<td>Principal</td>
<td><input id="principal"></td>
</tr>
<tr>
<td>Rate </td>
<td> <input id="rate"></td>
</tr>
<tr>
<td>Years </td>
<td> <input id="years"></td>
</tr>
<tr>
<td> Amount </td>
<td>
<input id="amount">
</td>
</tr>
<tr>
<td>Interest </td>
<td>
<input id="interest">
</td>
</tr>

<tr>
<td><button onclick="calculate()">Calculate</button></td>
<td><button onclick="clear()">Clear</button></td>
</tr>

</table>

</body>

</html>

---------------------------------------------------------------------

Sample Output:

HOW YOUR INVESTMENT GROWS Principal 2000 Rate 5 10 Year Amount 3257.79 Interest 1257.79 CalculateClear

Add a comment
Know the answer?
Add Answer to:
Modify the compound interest program presented in my JS Control Statements and Loops slides (p. 16-18)...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT