Advertisement
Luninariel

Financial Calculator - Compound Interest WIP

Nov 27th, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. Compound Interest is the portion of the program that calculates the Compound Interest on a loan
  3.  */
  4.  
  5. //Variables To Capture User Input
  6. const readline = require('readline');
  7. const rl = readline.createInterface({
  8.     input: process.stdin,
  9.     output: process.stdout
  10. });
  11.  
  12. //Function to Perform the compounding
  13. function compounder(p,r,m,t){
  14.     const rate = r / 100;
  15.     const rOVERm = rate / m;
  16.     const mTIMESt = m * t;
  17.     const end = p * (Math.pow(1 + rOVERm, mTIMESt));
  18.     console.log("\nThe Total amount after interest is $"+end.toFixed(2));
  19.     exit();
  20. }
  21.  
  22. //A Function To Allow Process Exiting
  23. function exit() {return process.exit(1);}
  24.  
  25. //A Function to detect invalid input
  26. function invalid() {console.log("\nIt Seems You Entered An Invalid Input, Please Try Again\n");}
  27.  
  28. //A Function to collect the principle of the loan
  29. function principle() {
  30.     rl.question('\nWhat is the Principal?', (PR_old) => {
  31.         let PR = parseFloat(PR_old);
  32.         if (isNaN(PR) === false && PR > 0) {
  33.             console.log("\nGot it, The Principle is: $"+PR);
  34.             rate(PR)
  35.         } else {
  36.             invalid();
  37.             principle();
  38.         }
  39.     });
  40. }
  41.  
  42. //A Function to collect the rate of the loan
  43. function rate(PR) {
  44.     rl.question('\nWhat is the Rate?', function (RT_old) {
  45.         let RT = parseFloat(RT_old);
  46.         if (isNaN(RT) === false && RT > 0) {
  47.             console.log("\nGot it, the rate is: "+RT+"%");
  48.             compounded(PR,RT)
  49.         } else {
  50.             invalid();
  51.             rate(PR);
  52.         }
  53.     });
  54. }
  55.  
  56. //A Function to ask the frequency that the item is compounded.
  57. function compounded(PR,RT) {
  58.     rl.question('\nHow many times per year is it compounded?', function (M_old) {
  59.         let M = parseInt(M_old);
  60.         if (isNaN(M) === false && M > 0) {
  61.             console.log("\nGot it, The interest is compounded "+M+" Times");
  62.             time(PR,RT,M)
  63.         } else {
  64.             invalid();
  65.             compounded(PR,RT);
  66.         }
  67.     });
  68. }
  69.  
  70. //A Function To Gather how many years the loan is for
  71. function time(PR,RT,M) {
  72.     rl.question('\nHow Many Years Is The Loan Over?', function (T_old) {
  73.         let T = parseInt(T_old);
  74.         if (isNaN(T) === false && T > 0) {
  75.             console.log("\nGot it, The Loan Is Over "+T+" Years");
  76.             compounder(PR, RT, M, T);
  77.             rl.close();}
  78.         else {
  79.             invalid();
  80.             time(PR,RT,M);
  81.         }
  82.     });
  83. }
  84.  
  85. //Exporting the Principle module
  86. module.exports = {
  87.     principle
  88. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement