# It's a functional code?

Hi!

So I saw this challenge and it was to difficult to find a solution for it, so I looked in the solution gived. There I saw an idea to continue the code by makind it converting roman numbers into arabic numbers. I tryed something, but it’s not totaly functional.

Can you give me a feedback for my code, please?
What do you think I need to do to make it better?

``````
function convertToRoman(num) {

let romans = ['M', 'CM', 'D', 'CD', 'C', 'XC', 'L', 'XL', 'X', 'IX', 'V', 'IV', 'I']
let arrabics = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1]
let romanic = ''
let number = 0

if (typeof num === 'number') {
for (let i = 0; i < arrabics.length; i++) {
while (num >= arrabics[i]) {
romanic += romans[i];
num -=arrabics[i]
}
}
console.log(romanic)
return romanic;
} else if (/[MDCLXVI]+/.test(num)) {

let roman = ['M', 'D', 'C', 'L', 'X', 'V', 'I']
let numeric = [1000, 500, 100, 50, 10, 5, 1]
let arr = []
arr = num.split('')

for (let i = 0; i < arr.length; i++) {

if (roman.indexOf(arr[i]) > roman.indexOf(arr[i+1])) {

if (roman.indexOf(arr[i+1]) === -1 )
return number += numeric[roman.indexOf(arr[i])];

number += numeric[roman.indexOf(arr[i+1])] - numeric[roman.indexOf(arr[i])];

}  else number += numeric[roman.indexOf(arr[i])]

}

}

else return 'Type a number or a valid roman numeral!'
}

convertToRoman('MDCLVI');

``````

User Agent is: `Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36`.

Challenge: Roman Numeral Converter

Hello~!

When you say it is not functional, what do you mean? It appears to pass all of the tests on my end.

Well… If i pass, for exemple, ‘CMMDIXV’ should be an invalid roman numeral… that’s why i believe is not totaly functional.

Though, I believe this is the only situation when it returns a false answer.

Your `return "Enter a valid roman numeral` statement only runs if I’ve entered something that contains an invalid character (and thus fails your regex test). If I enter `CMMDIXV`, the code calls the `else if` block which doesn’t seem to check if a Roman numeral is in the wrong order.

So what do you think that i should do? Modify the regex?

Or test to confirm that all of the letters are in the correct order.

How can i do that? The only thing i can think that can do that is regex

You’re already looping through the string to find where the letters are in the array, right?

1 Like

Still i can’t think on the solution that can resolv the problem… And by the way… aparently doesn’t work well. I just put ‘MMMCDXXIV’ and it gave me 3929, not 3424…

Because your array doesn’t account for the rules for 4 and 9.
It sees `IV` and reads 1 and 5, instead of 4.

I fixed the bug, but still i don’t know how to fix the bug with corect order.

This is the bug fixed:

``````if (roman.indexOf(arr[i]) > roman.indexOf(arr[i+1])) {

if (roman.indexOf(arr[i+1]) === -1 )
return number += numeric[roman.indexOf(arr[i])] ;

number -= numeric[roman.indexOf(arr[i])];

}  else number += numeric[roman.indexOf(arr[i])]
``````