Stuck on the Roman Numeral Converter Challenge

Hey,

I’v been stuck on this challenge for 3 or 4 days. I tried chat rooms but didn’t get that much help ,and then saw the challenge guide and used the hint to create two arrays, one for the Roman numerals and one for the decimal numerals. After that, I’m thinking of writing “if … else if” statements but can’t figure out what to put inside these statements.

Here’s the two arrays:

var romanArr = ["I", "IV", "V", "IX", "X", "XL", "L", "XC", "C", "CD", "D", "CM", "M"];
var decimalArr = [1, 4, 5, 9, 10, 40 ,50, 90 ,100, 400, 500, 900, 1000];

I would suggest thinking about the rules that govern how we write Roman Numerals.

For example, if a number is below the value of 4 what characters are used? What if the the value is higher than a 10, again what characters are being used and for which place value?

This same line of thinking also goes for our own numbering system but, we need not fall into that discussion.

Here’s a bit of pseudo-code to nudge you along:

if ONES-VALUES < 4 then:
ONES-VALUES-VAR = “I” times ONES-VALUES

So, if we gave the above pseudo 53 to convert, it would look at my ONES-VALUES, in this case 3 and set my ONES-VALUES-VAR to 3 x “I” or “III”.

I remembering being stuck on this one for a little while, at least a week. If you need more help don’t be afraid to ask.

Thanks for the reply.

I was thinking along the same lines, but the problem was I wasn’t able to translate it into code and there are many cases.

I just wrote a code using this way with a tip I got from someone from the chat today (using “%” and “/”). Then I got an infinite loop and had to delete my code.

I’ll try to write part of that code here:

function addRomanNum(n, c){
    var result="";
    for(var i = 0; i < n; i++){
        result += c;}
    return result;
}

function convertToRoman(num){
    var n; romanNum = "";
    while(num >= 1){
        if(num >= 1000){
            n = Math.floor(num / 1000);
            romanNum += addRomanNum(n, "M");
            num = num % 1000;
        }else if(num >= 900 && num < 100){
            romanNum += "CM";
            num = num % 900;
        } ... and so on ...

What do you think?

Did you put some Break on your ‘while’ loop?

No.

When and why should I use the break keyword?

And does that mean my approach is right (other than not using break)?

What I mean is, as long as the condition for your ‘while’ loop is met (in this case num>=1), the loop will not stop, thus the infinite loop.

For another approach, you can use ‘switch’ to pass the ‘num’ over various manipulation to the ‘num’ and it will execute only once.
for example:

switch (true) {

case num >= 500: /*do something to the num*/
  temp = Math.floor(num/500);
  if(num>=900) {
    roman = roman + "CM";
    num = num%900;
  } else {
    roman = putRom(temp,"D",roman);
    num = num%500;
  }

…/* and so on*/

pass the ‘num’ on every case, when num>=1000, num>=500, 100, 50, 10, 5, 1

I was thinking about using switch at first, but I decided not to after reading this: JavaScript switch with logical operators?

If you don’t put break on your switch, after a condition on a case is met(and after executing the following statement), it will continue testing against another case. My latter approach is kinda the same with putting it into a lot of if then else statement.
If you want to use while that;s okay too, but make sure that you can exit the loop, that is the value of your num will < 1, orl else it’ll be trapped in infinite loop. I was using while too at first, and then switch it to switch because I only need to compare my num value once against each case, (like num>=1000,500,and so on).

1 Like

Oh, I know, my finished code is a mess with all of these if/else statements that might’ve been cleaner with a switch statement but, live and learn.

As for your example, I’m not sure I understand what is going on with the modulo, is it just a reset for your “num” variable or is it serving some other purpose my sleepy brain isn’t seeing?

Also, in part due to my having had troubles with the dreaded infinite loop, I try to limit my reliance on while loops. Like tobive wrote above, they can be a bit tricky. But, if you’re comfortable with them then keep going the route you’ve started out on.

Lastly, is your code producing anything yet, even if it is the wrong answer?

Best of luck.

1 Like

The modulo is to return the non-converted part of the decimal numeral (e.g. 1200, after converting the 1000 to “M”, it returns 200, so the code could convert the remainder again, and so on …).

After spending all that time writing the if ... else if statements and getting the infinite loop, I called it a day and rage quit. I’ll try again tomorrow.

Best of luck to you too.

1 Like

Try “dissolving” the number in a array of numbers, something like:

var arr = ;
arr[0] = Math.floor(num/1000);
num = num - Math.floor(num/1000)*1000;
arr[1] = Math.floor(num/100);

I found it easier than using mod. And you can handle the resulting array with a switch inside a for.

Hope it helps in some way, I got stuck in this one for a while too. Good luck!

First off, you should consider whether or not you can separate the numbers individually before attempting to get the number to convert to roman numeral.
Trying to convert the whole number is rather difficult, since the roman numeral rules don’t apply to normal numbers.

For example: Given the number 15729. Are you able to separate them and print them out one number at a time. 1 5 7 2 9. If not, try doing this first. This will help you determine the final roman numeral based on their positions. (Position meaning, their place value.)

Next analyze the roman numeral problem. The biggest hint is on the “Roman Numeral” link provided in the challenge. It shows you a picture of how it breaks down the problem.

I think I’ve given you enough hints already. LOL