peter-ohara/intro_to_programming

[BUG] Doesn't work for numbers greater than or 100000 (one hundred thousand)

Closed this issue · 1 comments

@ohara-invent Depending on the size of the number, either extract_tens or extract_thousands throws an exception similar to:

100000: ./fiw.rb:54:in `extract_tens': undefined method `+' for nil:NilClass (NoMethodError)
    from ./fiw.rb:29:in `extract_thousands'
    from ./fiw.rb:23:in `nums_to_words'
    from ./fiw.rb:7:in `block in main'
    from ./fiw.rb:4:in `each'
    from ./fiw.rb:4:in `main'
    from ./fiw.rb:130:in `<main>'

The case above is the exception for 100_000.

@ohara-invent I took a look at the implementation of extract_tens (starting on L47):

def extract_tens(number)
  tens = ["", "ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"]
    if (number < 10)
      return extract_ones(number)
    elsif (number < 20)
      return extract_teens(number)
    elsif (number <= 100)
      return tens[number / 10] + " " + extract_ones(number % 10)
    end
end

if number is equal to 100 going into the elsif number <= 100 branch, then number/10 will be equal to 10, and that's what you use to index the tens array. But the last index of tens is 9 not 10, and so the program fails at that point. Please see if that's the case.