I am working on a Ruby exercism exercise and currently can't complete the last step which is returning the number of years it takes for a current balance amount to reach a target balance amount. I've tried using while and until (as suggsted by the exercise) but i don't get the number of years. I know using until doesn't return a value but i am struggling to work out how to produce the desired number. Would someone be able to steer me in the right direction please? Any help would be greatly appreciated!
Code is below:
module SavingsAccount
def self.interest_rate(balance)
if balance >= 0 && balance < 1000
interest_rate = 0.5
elsif balance >= 1000 && balance < 5000
interest_rate = 1.621
elsif balance >= 5000
interest_rate = 2.475
elsif balance < 0
interest_rate = 3.213
else
interest_rate = 0.500
end
return interest_rate
raise 'Please implement the SavingsAccount.interest_rate method'
end
def self.annual_balance_update(balance)
interest_rate = interest_rate(balance) / 100
interest_rate += 1
if balance < 0
return balance * (interest_rate)
else
return balance.abs * (interest_rate)
end
raise 'Please implement the SavingsAccount.annual_balance_update method'
end
def self.years_before_desired_balance(current_balance, desired_balance)
interest_rate = interest_rate(current_balance) / 100
interest_rate += 1
years = 0
until current_balance == desired_balance do
current_balance = current_balance * interest_rate
years += 1
if current_balance == desired_balance
return years
end
end
raise 'Please implement the SavingsAccount.years_before_desired_balance method'
end
end
I've checked the values up until the "until" keyword is used and everything looks ok. I've also tried to move the "return years" line out of the until block.
The question i am trying to solve is:
Implement the SavingsAccount.years_before_desired_balance method to calculate the minimum number of years required to reach the desired balance:
SavingsAccount.years_before_desired_balance(200.75, 214.88)
#=> 14
I've checked the values up until the "until" keyword is used and everything looks ok. I've also tried to move the "return years" line out of the until block. I'm expecting to return an integer for the number of years it takes for current balance to reach target balance based on interest rate.