# Roman Numeral Converter I'm stuck

Tell us what’s happening:

I’m stuck in this challenge for 2 days. I just don’t know what to do, I only knew that I should make 2 arrays, one with numerals and one with num but I don’t know what I should to do with them.

function convertToRoman(num) {
let numbers = [1,4,5,9,10,40,50,90,100,400,500,900,1000];
let numeral = [I,IV,V,IX,X,XL,L,XC,C,CD,D,CM,M];
}

convertToRoman(36);

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:61.0) Gecko/20100101 Firefox/61.0.

first question - do you know how to convert numbers to roman numerals if someone asks you to do it? If not, research that first.

for eg. if I said convert:
99 to roman numerals, you should know how to write it for yourself.

second question - if you know how to do it yourself, can you write down steps, clear ones, to explain it to someone else? If yes, write your steps down. Make sure they work by either asking someone else to go through them or you yourself make sure they work for every kind of number.

final question - do you know how to convert your steps to code?
This one may be the trickiest or easiest bit depending on how clear your steps were.

for eg. Here is my steps on how to convert a number that is between 0-9 to binary:

0 is just 0 in binary
1 is just 1 in binary
2 can be written as 2^1 so it is a 10 in binary
3 is 2+1 so add 10 to 1 and you get 11 which is the representation in binary.
4 is 2^2 so it is a 100 in binary
5 is 4+1 so it is a 100+1 which is 101 in binary
6 is 4+2 so it is 100+10 which is 110 in binary
7 is 4+3 so it is a 100+11 which is 111 in binary
8 is 2^3 so it is a 1000 in binary
9 is 8+1 so it is 1000+1 which is 1001 in binary

as you can see , there is a pattern above. The pattern is:
check if the number is a factor of 2, if it is , the representation is simply (ten times the power)
if it is not check if it is 1 or 0, these stay exactly the same
if it is none of the above, rerewrite the number as a factor of 2 plus a remainder . The binary representation of the remainder is added to the binary representation of the closest smaller factor of 2.
etc.

So I hope I’ve given you an idea about how to approach this challenge.
1- identify if you understand how to solve the problem yourself
2- write down simple steps and patterns to explain to someone else how to solve it
3- rewrite those steps as code.

Are you sure, because I did not understand your advice with roman number.
You create a binary converter.

I mean, I only know how to convert to numerals by using a guide. The whole numeral concept is new to me as I only use the very simple ones like 1 => I, 2 => II, 3 => III, beyond that, I don’t know except by using a page(which I use it btw)

Thank you for the advices. I hope that I make a full use of them.

hi @lubodrinka, I was trying to explain the idea behind algorithm creation.
I gave an example of what I would do if I was asked to convert a number 0-9 to binary.

I was hoping this would help the camper understand the method to creating a brand new algorithm.

you can look up how to convert number to roman numerals online. there are many guides on the subject.

once you fully understand it, you can work out an algorithm next.

Oh, I already use one: https://www.mathsisfun.com/roman-numerals.html

1 Like

Its not easy like that. Should be if you make an arrays with all possibilities in task, maybee zou don’ t scroll enough.
good way but you must notice the some symbols are repeating, you must make an alias.
how you write 3 in roman and just tell the code to do it but in some loop?You should understand

See if you can convert that guide to a written algorithm (pseudo-code if you want) first before starting to write ANY code. Post your algorithm here for us to review, so we can give you some pointers.

For many campers, developing an algorithm for this challenge is difficult. However, if you can break it into smaller pieces, it can be solved fairly easily. Try writing the algorithm first though.

But that is hardest examples., what I ever see. for example its A is one, B is two…,convert the ABCD to numeric output is 1234

That it’s 1 => I, 2 => II, 3 => III, now just continue, same in other number with roman rules

@lubodrinka
@hbar1st
@RandellDawson
Thank you so much for your advices. After 2 hours of serious thinking for the challenge. I thought of a solution and solved it! Thank you so much, if not for you, I would probably gave up and copied someone else’s code instead of developing further my skills.
This is my solution:

function convertToRoman(num) {
const numbers = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1];
const numeral = ['M', 'CM', 'D', 'CD', 'C', 'XC', 'L', 'XL', 'X', 'IX', 'V', 'IV', 'I'];
const arr = [];
let renum = num;
while (arr.reduce((before, after) => before + after, 0) < num) {
const EL = numbers.findIndex(element => renum - element >= 0);
arr.push(numbers[EL]);
renum -= numbers[EL];
}
return arr.map(element => numeral[numbers.indexOf(element)]).join('');
}

convertToRoman(36);

Basically, it transfoms the number into multiple max sum possible like (55 => 50 + 5) then finally, making a new array by transforming into number into their equivant in the numerals array and then join them to get a string and the final result.

Now, I finally can go to other libraries/frameworks after finishing this last(to me) project. Again, thank you!

2 Likes

good for you for listening to the advice and making effective use of it. Some people can’t even listen… Happy coding!

1 Like

That is great that you solved the challenge. It really it is not necessary to post your final solution. In fact, we prefer you not to post full working solutions to the challenges. Trust me, we believe you solved it, if you say your did.

It take ten minutes to find the logic of code. I never thought the function should be in while condition. I took it different way I make two groups of romans numbers, because the princip is the same. without debbuger the return arr.map might be (e,i)=> numeral[i], because they are in order because you use join.

Actually, I posted the code for anyone who faces the same problem.

I don’t think I understand what you’re saying.

So if is join in the end that means the order should be by index instead numbers.indexOf(element)

In the future, a description of your algorithm or if you want to post a specific section of code and walk everyone through that, that would be fine. Just try to avoid posting full working solutions.

Thank you.

I used findIndexOf to find the biggest number possible that could be reduced from num for 0 or more to remain after that. I’m sorry but I don’t think I understand you.