3

I am making a program to change decimal numbers to Roman numerals. I am close to making numbers 1–8 work, but for some reason 3 won’t show up. I just want to get this problem solved. Once I do, I feel like I can build the rest of the program. Keep in mind I’m only in an intro to programming class, so I’m sure there are much better ways to solve this than how I’m approaching it, so far I’m just using what I’ve learned.

var romanize = function(userNumber) {
  var rome = [];
  for (var i = 0; i <= userNumber; i++) {
    if (userNumber >= 5) {
      rome.push("V");
      userNumber = (userNumber % 5);
    } else if (userNumber === 4) {
      return "IV";
    } else if (userNumber < 3) {
      rome.push("I");
    }
  }
  return rome
};
Sebastian Simon
  • 18,263
  • 7
  • 55
  • 75
Scott M
  • 187
  • 2
  • 11
  • 3
    You want <= 3, not < 3. – John Clifford Mar 08 '16 at 15:13
  • userNumber < 3 should be userNumber <= 3 – Mark Chorley Mar 08 '16 at 15:14
  • Programming this way will only allow you get the roman numerals of a number up to a certain point. The true challenge is making it work for any number. Assume you have a series of value/letter pairs to work with (i.e. `[[1, 'I'], [5, 'V'], [10, 'X'], ..., [1000, 'M']]`). – Neil Mar 08 '16 at 15:19
  • The VIII to IX switch is a potential trap as well. – John Clifford Mar 08 '16 at 15:21
  • You're doing pretty well. Not only a working program, but also a well-asked stackexchange question. You're already ahead of a lot of coders – Chris Lear Mar 08 '16 at 15:25
  • We know that biggest roman number is ` MMMCMXCIX` which is equal to `3999` if you are looking for fast solution generate all roman numbers push them to array and print requested one. – whd Mar 08 '16 at 16:00
  • If it helps Scott, I've just written a C# variant of this which does 1-3999, so if you have any further issues with it I should be able to assist you. – John Clifford Mar 08 '16 at 16:28

0 Answers0