I’m a bit stuck on the Roman Numeral Challenge.
This is the code I wrote, it seems to return the matching answers but its not passing the challenge. I’m still very new to Javascript and can’t quite figure out whats causing the issues. Any feedback would be greatly appreciated!
let romanNumerals = [ ]; (empty array)
function convertToRoman(num) {
const roman = ['M', 'CM', 'D', 'CD', 'C', 'XC', 'L', 'XL', 'X', 'IX', 'V', 'IV', 'I'];
const numbers = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1];
if (num <= 0) {
return romanNumerals.join('')
} else{
let index = numbers.findIndex(function(number) {
return number <= num;
})
romanNumerals.push(roman[index]);
convertToRoman(num - numbers[index]);
}
return romanNumerals.join('')
}
convertToRoman(3)
Challenge: Roman Numeral Converter
Link to the challenge: