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?
Your code so far
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');
Your browser information:
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
Link to the challenge: