Hiya guys,
I’m studying JS – left lot what to be taught…
Now I attempt to make calculator, witch calculate the consequence, witch I get after some years and present linear in chart (chart works).
In a kinds I enter amount of cash, witch I’ll pay periodically, select durations (yearly, quarterly, two occasions a 12 months, month-to-month).
Then I enter the variety of years for a way lengthy I need to make investments.
And eventually I enter charges.
Shall we say:
I make investments for 2 years, make investments 100 Eur month-to-month with 8% charges.
So after 1 12 months I invested 1200 Eur. and will have 1296 Eur.
After 2nd years I ought to have 2695.68 Eur. (1296 (from 1st 12 months) + 1200 (invested month-to-month) + charges).
I attempt to discover what I wrote flawed in a code (and formulation), that its calculating dangerous outcomes (and chart seems dangerous – by dangerous outcomes of calculating).
Might you please examine my perform code:
perform calculateGrowth(e)
e.preventDefault();
knowledge.size = 0;
labels.size = 0;
let development = 0;
strive
const quantity = parseInt(Quantity.worth);
const interval = parseInt(years.worth);
const curiosity = parseInt(charges.worth);
const comp = parseInt(compound.worth);
for(let i = 1; i <= interval; i++)
const closing = (quantity * comp / 100 * curiosity, comp * i);
knowledge.push(toDecimal(closing, 2));
labels.push("Yr " + i);
development = toDecimal(closing, 2);
//
message.innerText = `You're going to get $development Eur. after $interval years`;
drawGraph();
catch (error)
console.error(error);
I believe the issue is in my formulation…
Or simpler solution to calculate factor what I would like and also you suggest me to vary my perform one way or the other?!
P.S. I used to be googling, tried one other formulation with no proper consequence.
Which library are you utilizing for the toDecimal perform?
There are lots of shifting elements to that code, and plenty of assumptions that may be wrongly made about how you plan for issues to work.
Are you able to please put collectively a demo model of the poorly calculated code in order that we are able to see it working in motion, and supply sufficient preliminary enter for us to precisely expertise the issue?
You’ll not have invested €1200 all through all the first 12 months so the curiosity won’t be €96.
Assuming curiosity is calculated for every month however solely added to capital yearly, the curiosity for the primary 12 months might be €52.
Thanks for quick reply.
Sorry… right here is full code:
const context = doc.getElementById("data-set").getContext("2nd");
let line = new Chart(context, );
//Values from the shape
const quantity = doc.getElementById("quantity");
const years = doc.getElementById("years");
const charges = doc.getElementById("charges");
const compound = doc.getElementById("compound");
//Messge
const message = doc.getElementById("message");
//The calculate button
const button = doc.querySelector(".input-group button");
//Connect an occasion listener
button.addEventListener("click on", calculateGrowth);
const knowledge = [];
const labels = [];
perform calculateGrowth(e)
e.preventDefault();
knowledge.size = 0;
labels.size = 0;
let development = 0;
strive
const preliminary = parseInt(quantity.worth);
const interval = parseInt(years.worth);
const curiosity = parseInt(charges.worth);
const comp = parseInt(compound.worth);
for(let i = 1; i <= interval; i++)
const closing = (preliminary * comp / 100 * curiosity, comp * i);
knowledge.push(toDecimal(closing, 2));
labels.push("Yr " + i);
development = toDecimal(closing, 2);
//
message.innerText = `Get $development eur after $interval years`;
drawGraph();
catch (error)
console.error(error);
perform drawGraph()
line.destroy();
line = new Chart(context,
sort: 'line',
knowledge:
labels,
datasets: [
label: "Prieaugis",
data,
fill: true,
backgroundColor: "rgba(12, 141, 0, 0.7)",
borderWidth: 3
]
);
perform toDecimal(worth, decimals)
return +worth.toFixed(decimals);
Thanks in your minds
Why not? 😮 If I make investments 100 every month, it must be 1200 on the finish of the 12 months, and if the rate of interest is 8%, 1200 ought to enhance by 96 (1200 + 96 = 1296).
In case you invested €1200 initially of the 12 months and made no month-to-month funds you’ll anticipate €96 curiosity.
In case you’re making an attempt to make this an correct illustration of curiosity, for instance from a financial savings account, you need to use the formulation for Compound Curiosity. It’s nothing like what you’ve coded. You could possibly Google “compound curiosity formulation” it and get the main points.
You haven’t invested 1,200 for 12 months. You’ve invested 100 for 12 months, 200 for 11 months, … 1,200 for 1 month.
1 Like
Begin of Month 1: Make investments 100.00
Begin of Month 2: 100.00*1.08 = 108.00 + Make investments 100.00, whole 208.00
Begin of Month 3: 208*1.08 = 224.64 + Make investments 100.00, whole 324.64
Begin of Month 4, 324.64*1.08 = 350.6112 + Make investments 100, whole 450.6112
Begin of Month 5: 450.6112*1.08 = 486.660096 + Make investments 100, whole 586.660096
Is that an correct illustration of the values that you really want your code to attain?
The 8% rate of interest can be per 12 months, not monthly.
Financial savings acccounts often add curiosity to capital on the anniversary of opening the account, not every month. Curiosity doesn’t earn curiosity till it’s added to capital.
I calculate the stability on the finish of the primary 12 months can be €1252 (simply earlier than the thirteenth cost into the account) and on the finish of the second 12 months can be €2604.16.
With paying into the account each month, the same old compound curiosity formulation wouldn’t apply.
Thanks Paul_Wilkins,
No, I would like calculate charges after 12 months (trigger I can select frequency (how usually I need to pay) – yearly, month-to-month, two occasions a 12 months).
I discovered formulation in google, tried it in console – it matches for me completely: x = (p + x) * (1 + r / 100);
All code is:
console.clear();
(perform calc()
//frequency is how usually you pay - month-to-month, yearly, and many others.
let frequency = 12;
//sum is sum of normal cost
let sumP = 100;
// p is principal quantity
let p = frequency * sumP;
// t is time
let t = 2;
// r is rate of interest
let r = 8;
// x is the ultimate curiosity
let x = p;
for (let i = 1; i < t; i++)
if (i == 1)
x *= 1 + r / 100;
x = (p + x) * (1 + r / 100);
console.log(x.toFixed(2));
)();
I attempt to discover an issue right here (tried to adapt formulation I discovered and tried) however perform calculate sth flawed
perform calculateGrowth(e)
e.preventDefault();
knowledge.size = 0;
labels.size = 0;
let development = 0;
strive
const preliminary = parseInt(quantity.worth);
const interval = parseInt(years.worth);
const curiosity = parseInt(charges.worth);
const comp = parseInt(compound.worth);
for(let i = 1; i < interval; i++)
//x = (p + x) * (1 + r / 100);
const closing = (comp * preliminary + comp * preliminary) * (1 + curiosity / 100);
knowledge.push(toDecimal(closing, 2));
labels.push("Yr " + i);
development = toDecimal(closing, 2);
//
message.innerText = `Get $development eur after $interval years`;
drawGraph();
catch (error)
console.error(error);
perform is ~ easy, however don’t see the place is the issue… %o
You may flip that working perform in to at least one that takes perform parameters, and simply use that out of your code.
// for instance, to calculate 12-monthly funds of $100 for two years at 8%
// name it as follows: const whole = calculateInvestment(12, 100, 2, 8)
perform calculateInvestment(frequency, cost, time, interestRate)
let principal = frequency * cost;
let whole = principal;
for (let i = 1; i < time; i++)
if (i == 1)
whole *= 1 + interestRate / 100;
whole = (whole + principal) * (1 + interestRate / 100);
return whole;
Your code can then name that calculateInvestment perform to correctly calculate the quantity, after which present it as a graph.
That calculateInvestment perform will also be simplified to the next:
// for instance, to calculate 12-monthly funds of $100 for two years at 8%
// name it as follows: const whole = calculateInvestment(12, 100, 2, 8);
perform calculateInvestment(frequency, cost, time, interestRate)
const principal = frequency * cost;
let whole = 0;
whereas (time--)
whole += principal;
whole *= 1 + interestRate / 100;
return whole;
Or, utilizing an object for the parameters, in order that the parameters are simpler to know when calling the perform:
// for instance, to calculate 12-monthly funds of $100 for two years at 8%
// name it as follows:
// const whole = calculateInvestment(
// frequency: 12,
// cost: 100,
// time: 2,
// interestRate: 8
// );
perform calculateInvestment(frequency, cost, time, interestRate)
const principal = frequency * cost;
let whole = 0;
whereas (time--)
whole += principal;
whole *= 1 + interestRate / 100;
return whole;
If it is a month-to-month financial savings account, curiosity can be labored out month-to-month not yearly. I anticipate in apply curiosity is labored out day by day. As talked about beforehand on this thread, it’s common for curiosity to be added yearly on the anniversary of account opening (or when an account is closed).
Right here is my very fundamental JavaScript calculation for €100 added month-to-month with 8% per 12 months rate of interest:
1 Like