From 5e04e5393a5bf1403707d3e43c0652efccca1d80 Mon Sep 17 00:00:00 2001 From: Eric Douglas Date: Sun, 21 Jun 2015 19:47:24 -0300 Subject: [PATCH] Intro to CS - v02 problem set 01.02 - final --- .../src/01-02-problem-set.js | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/computer-science/01-introduction-to-cs-and-programming-mit/src/01-02-problem-set.js b/computer-science/01-introduction-to-cs-and-programming-mit/src/01-02-problem-set.js index 25b896e..17ed780 100644 --- a/computer-science/01-introduction-to-cs-and-programming-mit/src/01-02-problem-set.js +++ b/computer-science/01-introduction-to-cs-and-programming-mit/src/01-02-problem-set.js @@ -36,19 +36,24 @@ prompt.get([ var monthsNeeded = 0; var totalBalance = initialBalance; var monthInterest = interest / 12; - var monthPayment = Math.ceil( initialBalance * ( 1 + interest ) / 120 ) * 10; - // (( balance * ( interest ) / 12 ) / 10 ) * 10 + var monthPayment = 10; while ( totalBalance > 0 ) { - totalBalance = ( totalBalance * ( 1 + monthInterest ) - monthPayment ).toFixed( 2 ); + totalBalance = ( totalBalance * ( 1 + monthInterest ) - monthPayment ); monthsNeeded += 1; + + if ( monthsNeeded >= 12 ) { + monthPayment += 10; + totalBalance = initialBalance; + monthsNeeded = 0; + } } console.log( '======= RESULT ======='); console.log( 'Monthly payment to pay off debt in 1 year:', monthPayment ); console.log( 'Number of months needed:', monthsNeeded ); - console.log( 'Balance', totalBalance ); + console.log( 'Balance', totalBalance.toFixed( 2 )); });