0

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.

d709859
  • 3
  • 1

1 Answers1

1

Due to the way floating point numbers work in computers, using == almost never does what you want. Also since you're looking for a mininum number and not exact, you might want to use > instead of ==.

This is the condition you need:

if current_balance >= desired_balance

Note that for the same reason the until condition would never fire, so you can just use loop do.

iBug
  • 35,554
  • 7
  • 89
  • 134
  • Thanks, i have used loop do now and it is working. Thanks for taking the time to help me out! – d709859 Jun 21 '23 at 13:40
  • Note also that if you use `loop do ... end` you can use `break` to return a value from the loop. If the loop is the last expression in your method, this will then be the return value of the method. – Chris Jun 21 '23 at 18:53