DEV Community

loading...
Cover image for Day 2 of JavaScriptmas - Deposit Profit Solution

Day 2 of JavaScriptmas - Deposit Profit Solution

Sekti
Keep moving forward. Learn well not learn fast.
・1 min read

Day 2 challenge is calculate the money on the bank. Each year the money increases with same growth rate.

For instance, $100 deposit with $20 growth rate. How many year the money will be about $170 in the bank?

At year 0: $100
At year 1: $120
At year 2: $144
At year 3: $172.8

So there is the JavaScript solution

function depositProfit(deposit, rate, threshold) {

    // initial yearCount 0
    // deposit year 0
    // 
    // loop while threshold >= deposit
    //      rate = deposit * 20/100
    //      deposit += rate
    //      initial year += 1
    // return year
    // 

    let yearCount = 0;
    let depositCount = deposit;

    while(threshold >= depositCount) {
        let rateCount = depositCount * (rate/100);
        depositCount += rateCount;
        yearCount += 1;
    } 
    return yearCount;
}

Enter fullscreen mode Exit fullscreen mode

Discussion (0)