Calculate compound interest in JavaScript.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

50 lines
1.2 KiB

"use strict";
//Licensed under the Apache License 2.0. See license file for information.
// Ardon Compound Interest Calculator v1.0.0 Alpha
// Written by Josh Mudge
// Ad Mejorem Dei Glorium
function calculate() {
var numReg = /^\d+$/;
var final = 0;
var i = 0;
var contribution = 0;
// Get user input
var amount = document.getElementById("amount").value;
var time = document.getElementById("time").value;
var rate = document.getElementById("rate").value;
var contribution = document.getElementById("contribution").value;
if (amount.match(numReg))
{
//Do nothing.
}
else
{
//Tells them to enter only numbers.
alert("Please enter only numbers in this field.");
}
var rate = rate * 0.01
//console.log(rate)
var interest = amount * rate
//console.log(interest)
var final = amount;
for (i = 0; i < time; i++) {
var final = +final + (+final * +rate)
console.log(final)
var final = +final + +contribution
console.log(final)
}
//console.log("Test")
//console.log(final)
var final = final.toFixed(2);
//Show result
alert("You final investment amount is $" + final);
document.getElementById("interest").innerHTML = "Your final investment amount is $" + final;
}