// My second attempt, once I remembered the plus '+ 1' part to work out percentages
// function depositProfit(deposit, rate, threshold) {
// let years = 0;
// for (let i = 1; deposit < threshold; i++) {
// deposit = deposit * (rate / 100 + 1);
// years = i;
// }
// return years;
// }
function depositProfit(deposit, rate, threshold) {
// Change to percentage
if (rate < 10) {
rate = "1.0" + rate
} else {
rate = "1." + rate
}
// Change to float
rate = parseFloat(rate);
// Add interest to deposit
let compoundIterest = deposit * rate
let years = 1;
if (compoundIterest < threshold) {
for (let i = 2; compoundIterest <= threshold; i++) {
compoundIterest = compoundIterest * rate
years++
}
}
return years;
}
/**
* Test Suite
*/
describe('depositProfit()', () => {
it('returns number of years it will take to hit threshold based off of deposit & rate', () => {
// arrange
const deposit = 100;
const rate = 20;
const threshold = 170;
// act
const result = depositProfit(deposit, rate, threshold)
// log
console.log("result: ", result);
// assert
expect(result).toBe(3);
});
});