From c8587050fa8f2dec57bd1f274f1e09996d209762 Mon Sep 17 00:00:00 2001 From: Eric Douglas Date: Sat, 20 Jun 2015 19:04:11 -0300 Subject: [PATCH] Intro to CS - problem set 01.01 - finish problem set 01.01 --- .../src/01-01-problem-set.js | 30 +++++++++++++------ 1 file changed, 21 insertions(+), 9 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 ea6bae2..2827f9d 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 @@ -42,23 +42,30 @@ prompt.get([ } ], function( err, results ) { + console.log( results ); + // constants var PERIOD = 12; // input variables - var initialBalance = parseInt( results.balance, 10 ); - var interest = parseInt( results.interest, 10 ); - var minPayment = parseInt( results.minimum, 10 ); + var initialBalance = parseFloat( results.balance, 10 ); + var interest = parseFloat( results.interest, 10 ); + var minPayment = parseFloat( results.minimum, 10 ); + + console.log( initialBalance ); + console.log( interest ); + console.log( minPayment ); // output variables var month = 1; var minMonthlyPayment = 0; var principalPaid = 0; - var actualBalance = 0; + var actualBalance = initialBalance; // helper variables - var actualMinPayment; - var actualInterest; + var actualMinPayment = 0; + var actualInterest = 0; + var totalPaid = 0; while( month <= PERIOD ) { @@ -66,14 +73,19 @@ prompt.get([ actualInterest = ( interest / 12 ) * actualBalance; principalPaid = actualMinPayment - actualInterest; actualBalance -= principalPaid; + totalPaid += actualMinPayment; console.log( 'Month:', month ); - console.log( 'Minimum monthly payment:', actualMinPayment ); - console.log( 'Principal paid:', principalPaid ); - console.log( 'Remaining balance', actualBalance, '\n' ); + console.log( 'Minimum monthly payment:', actualMinPayment.toFixed( 2 )); + console.log( 'Principal paid:', principalPaid.toFixed( 2 )); + console.log( 'Remaining balance', actualBalance.toFixed( 2 ), '\n' ); month += 1; } + console.log( '======= RESULT ======='); + console.log( 'Total amount paid:', totalPaid.toFixed( 2 )); + console.log( 'Remaining balance', actualBalance.toFixed( 2 ), '\n' ); + });