diff --git a/ch09-writing-your-own-methods/ask.rb b/ch09-writing-your-own-methods/ask.rb index 01716eb35..c543d30d0 100644 --- a/ch09-writing-your-own-methods/ask.rb +++ b/ch09-writing-your-own-methods/ask.rb @@ -1,3 +1,9 @@ def ask question - # your code here -end \ No newline at end of file + while true + puts question + reply = gets.chomp.downcase + return true if reply == 'yes' + return false if reply == 'no' + puts 'Answer yes or no' + end +end diff --git a/ch09-writing-your-own-methods/old_school_roman_numerals.rb b/ch09-writing-your-own-methods/old_school_roman_numerals.rb index ca6589f2d..bc9ea8390 100644 --- a/ch09-writing-your-own-methods/old_school_roman_numerals.rb +++ b/ch09-writing-your-own-methods/old_school_roman_numerals.rb @@ -1,3 +1,13 @@ def old_roman_numeral num - # your code here -end \ No newline at end of file + + roman_array = [] + roman_array << 'M' * (num / 1000) + roman_array << 'D' * (num % 1000 / 500) + roman_array << 'C' * (num % 500 / 100) + roman_array << 'L' * (num % 100 / 50) + roman_array << 'X' * (num % 50 / 10) + roman_array << 'V' * (num % 10 / 5) + roman_array << 'I' * (num % 5 / 1) + return roman_array.join + +end diff --git a/ch09-writing-your-own-methods/roman_numerals.rb b/ch09-writing-your-own-methods/roman_numerals.rb index 5c93b59ac..14bd7fbd8 100644 --- a/ch09-writing-your-own-methods/roman_numerals.rb +++ b/ch09-writing-your-own-methods/roman_numerals.rb @@ -1,3 +1,26 @@ def roman_numeral num - # your code here -end \ No newline at end of file + values = [ + [1000, 'M'], + [900, 'CM'], + [500, 'D'], + [400, 'CD'], + [100, 'C'], + [50, 'L'], + [40, 'XL'], + [10, 'X'], + [9, 'IX'], + [5, 'V'], + [4, 'IV'], + [1, 'I'] + ] + new_roman_array = "" + + values.each do |number, roman| + while num >= number + new_roman_array << roman + num -= number + end + end + + new_roman_array +end