# JavaScript Algorithms and Data Structures Projects - Roman Numeral Converter

Tell us what’s happening:
Help! I really don’t understand what my problem is. I don’t know why it can’t pass a single test.

``````function convertToRoman(num) {
let result = "";

if(num >= 1000){
let count = Math.round(num/1000);
num -= count * 1000;
result.concat("M".repeat(count)).concat(convertToRoman(num));
}

else if(num >= 900){
num -= 900;
result.concat("CM").concat(convertToRoman(num));
}

else if(num >= 500){
num -= 500;
result.concat("D").concat(convertToRoman(num));
}

else if(num >= 400){
num -= 400;
result.concat("CD").concat(convertToRoman(num));
}

else if(num >= 100){
let count = Math.round(num/100);
num -= count * 100;
result.concat("C".repeat(count)).concat(convertToRoman(num));
}

else if(num >= 90){
num -= 90;
result.concat("XC").concat(convertToRoman(num));
}

else if(num >= 50){
num -= 50;
result.concat("L").concat(convertToRoman(num));
}

else if(num >= 40){
num -= 40;
result.concat("XL").concat(convertToRoman(num));
}

else if(num >= 10){
let count = Math.round(num/10);
num -= count * 10;
result.concat("X".repeat(count)).concat(convertToRoman(num));
}

else if(num == 9){
result.concat("IX");
}

else if(num >= 5){
num -= 5;
result.concat("V").concat(convertToRoman(num));
}

else if(num == 4){
result.concat("IV");
}

else if(num >= 1){
result.concat("I".repeat(num));
}

else if(num == 0) result.concat("");

return result;
}

convertToRoman(36);
``````

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

Challenge: JavaScript Algorithms and Data Structures Projects - Roman Numeral Converter

`concat` is an array method. You are working with strings. You need to use the appropriate method/operator to add to a string.

But I checked on W3C schools and it says concat can be used to join strings.

What will happen if `num = 1900`?

Oops I suddenly realised that every time when it gets into a recursion the result is set to an empty string. Stupid me. But I don’t know where to set the initial value of the result variable.

There isn’t any recursion here, and you don’t need any.

1 Like

Indeed. I have realised that there’s no need for recursion. Thank you very much!

Ya, you’re right, my bad, you can use `concat` with strings, it’s just not something you see a lot because `+` is so much more convenient.

1 Like

Thanks for helping me guys. I have figured out what my problem is. The first problem is that, as [JeremyLT] pointed out, there’s no need for recursion. The second problem is that I should use Math.floor() rather than Math. round() to get the count variable. Now I have passed the challenge.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.