From 2cd2e03d49d9bb01547f04e0fdaa23a184484633 Mon Sep 17 00:00:00 2001 From: Eric Douglas Date: Sat, 20 Jun 2015 18:43:37 -0300 Subject: [PATCH] Intro to CS - problem set 01.01 - set all variables --- .../src/01-01-problem-set.js | 29 ++++++++++++------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/computer-science/01-introduction-to-cs-and-programming-mit/src/01-01-problem-set.js b/computer-science/01-introduction-to-cs-and-programming-mit/src/01-01-problem-set.js index b903e80..ea6bae2 100644 --- a/computer-science/01-introduction-to-cs-and-programming-mit/src/01-01-problem-set.js +++ b/computer-science/01-introduction-to-cs-and-programming-mit/src/01-01-problem-set.js @@ -30,15 +30,15 @@ prompt.start(); prompt.get([ { name : 'balance', - description : 'Outstanding balance' + description : 'Enter the outstanding balance on your credit card' }, { name : 'interest', - description : 'Annual interest rate' + description : 'Enter the annual credit card interest rate as a decimal' }, { name : 'minimum', - description : 'Minimum monthly payment rate' + description : 'Enter the minimum monthly payment rate as a decimal' } ], function( err, results ) { @@ -46,9 +46,9 @@ prompt.get([ var PERIOD = 12; // input variables - var initialBalance = results.balance; - var interest = results.interest; - var minPayment = results.minimum; + var initialBalance = parseInt( results.balance, 10 ); + var interest = parseInt( results.interest, 10 ); + var minPayment = parseInt( results.minimum, 10 ); // output variables var month = 1; @@ -56,14 +56,23 @@ prompt.get([ var principalPaid = 0; var actualBalance = 0; - while( month < PERIOD ) { + // helper variables + var actualMinPayment; + var actualInterest; - let actualMinPayment = minPayment * actualBalance; - let actualInterest = ( interest / 12 ) * actualBalance; + while( month <= PERIOD ) { + + actualMinPayment = minPayment * actualBalance; + actualInterest = ( interest / 12 ) * actualBalance; + principalPaid = actualMinPayment - actualInterest; + actualBalance -= principalPaid; console.log( 'Month:', month ); console.log( 'Minimum monthly payment:', actualMinPayment ); - console.log( 'Principal paid:', ); + console.log( 'Principal paid:', principalPaid ); + console.log( 'Remaining balance', actualBalance, '\n' ); + + month += 1; }