Good work on this! Your code works, it successfully converts Arabic numbers into Roman numerals. Success!
Some notes:
-
This challenge is testing your ability to utilize hashes and the
/
and%
operators, which you do well. -
How could you better utilize these operators in your code? Here is a really short solution that further taps into the capabilities of
/
and%
:def to_roman(num) roman_num = {1000 => "M", 900 => "CM", 500 => "D",400 => "CD",100 => "C",90 => "XC" ,50 => "L",40 => "XL", 10 => "X",9=>"IX", 5 => "V",4=>"IV",1 => "I" } roman_num_string = "" roman_num.each do | k, v | roman_num_string << v * (num/k) num = num % k end return roman_num_string end
Take a look at this closely to see how it renders unnecessary the need for any conditional statements.
Keep at it, you are doing well! Your code works, just study ways like this to make your code cleaner and more efficient.
Any questions let me know.
-Phil