Advertisement
Luninariel

Financial Calculator - Simpleinterest WIP

Nov 27th, 2019
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. Simple interest is the portion of the program that calculates the simple interest on a loan.
  3.  */
  4. //Variables used to capture user inputs.
  5. const readline = require('readline');
  6. const rl = readline.createInterface({
  7.     input: process.stdin,
  8.     output: process.stdout
  9. });
  10.  
  11. //A Function To Allow Process exiting
  12. function exit() {return process.exit(1);}
  13. function invalid() {console.log("\nIt Seems You Entered An Invalid Input, Please Try Again\n");}
  14.  
  15. // A Function to actually process the loan and return the amount of interest and the amount after interest
  16. function processLoan(PR,IN,PY) {
  17.     let Interest = IN / 100;
  18.     let A = PR*(1+(Interest*PY));
  19.     console.log("\nThe Amount of interest is: $",(A-PR).toFixed(2));
  20.     console.log('\nThe Estimated Amount After interest Is: $', A.toFixed(2));
  21.     exit();
  22. }
  23.  
  24. //A Function to gather the value of the principle
  25. function principle() {
  26.     rl.question('\nWhat is the Principal of the loan? ', function (PR_OLD) {
  27.         let PR = parseFloat(PR_OLD);
  28.         if (isNaN(PR) === false && PR > 0) {
  29.             console.log(`\nGot it, The Principal Amount is: $${PR}`);
  30.             interest(PR)
  31.         } else {
  32.             invalid();
  33.             principle();
  34.         }
  35.     });
  36. }
  37.  
  38. //A Function To Gather The Interest Rate From The User
  39. function interest(PR){
  40.     rl.question('\nWhat is the interest rate? ', function (IN_OLD) {
  41.         let IN = parseFloat(IN_OLD);
  42.         if (isNaN(IN) === false && IN > 0) {
  43.             console.log("\nGot it, The Interest Rate Is:",IN,"%");
  44.             payments(PR,IN)
  45.         } else {
  46.             invalid();
  47.             interest(PR);
  48.         }
  49.     });
  50. }
  51.  
  52. //A Function To Gather How Many Monthly Payments There Are From The User
  53. function payments(PR,IN){
  54.     rl.question("\nHow many Monthly payments are there?", function (PY_OLD) {
  55.         let PY = parseInt(PY_OLD);
  56.         if (isNaN(PY) === false && PY > 0) {
  57.             console.log("\nGot it, There Are: ",PY,"Monthly Payments");
  58.             let PYout = PY/12;
  59.             processLoan(PR,IN,PYout);
  60.         } else {
  61.             invalid();
  62.             payments(PR,IN);
  63.         }
  64.         });
  65. }
  66.  
  67. //Exporting the module for its use in Main.
  68. module.exports = {
  69.     principle,
  70. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement