Menu

Post image 1
Post image 2
1 / 2
0

I finally understood compound interest by writing 8 lines of JavaScript

DEV Community·levent çelik·about 1 month ago
#9MxScSqm
Reading 0:00
15s threshold

Compound interest is one of those concepts I had nodded along to since high school without really feeling. The formula made sense on paper, the wikipedia chart looked impressive, and yet whenever a friend asked me "so what does 6% over 30 years actually look like?" I had to wave my hands. This weekend I sat down and wrote it out as code. That single exercise did more for my intuition than a decade of half-attentive reading. The formula, with names that mean something The textbook form is: A = P * (1 + r/n)^(n*t) Enter fullscreen mode Exit fullscreen mode Which is fine if you already understand it. Here is the same thing as a function with names a beginner can read: function futureValue ({ principal , annualRate , compoundsPerYear , years }) { const r = annualRate / compoundsPerYear ; const n = compoundsPerYear * years ; return principal * Math . pow ( 1 + r , n ); } console . log ( futureValue ({ principal : 10000 , annualRate : 0.06 , compoundsPerYear : 12 , years : 30 , })); // 60225.75...…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.

Read More