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.

Your code so far

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

  if(num >= 1000){
    let count = Math.round(num/1000);
    num -= count * 1000;

  else if(num >= 900){
    num -= 900;

  else if(num >= 500){
    num -= 500;

  else if(num >= 400){
    num -= 400;

  else if(num >= 100){
    let count = Math.round(num/100);
    num -= count * 100;

  else if(num >= 90){
    num -= 90;

  else if(num >= 50){
    num -= 50;

  else if(num >= 40){
    num -= 40;

  else if(num >= 10){
    let count = Math.round(num/10);
    num -= count * 10;

  else if(num == 9){

  else if(num >= 5){
    num -= 5;

  else if(num == 4){

  else if(num >= 1){

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

  return result;


Your browser information:

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

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

Link to the challenge:

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.