Roman Numeral Converter and switch not working

Tell us what’s happening:
I understood in theory how to do it with long switch statements but i don’t get why while execution code it pass switch statements without reaction. I showed only part of code because another part following the same scenario

Your code so far

function convertToRoman(num) {
    let rom = num.toString().split("");
    let newrom = [];
    let i = 0;
    let c = "";
    if (rom.length > 3) {
      while (i < rom[rom.length-4]) {
        newrom.push("M");
        i++;
      }
    }
  
    else if (rom.length > 2) {
      
      switch (rom[rom.length-3]) {
        case 1: c.push("C"); /*c = "C"; it also not working */ 
        break;
        case 2: c = "CC";
        break;
        case 3: c = "CCC";
        break;
      
      }
    }
    
 return console.log(newrom);
}

convertToRoman(9123);

Your browser information:

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

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/javascript-algorithms-and-data-structures-projects/roman-numeral-converter

Before tackling the code, you are missing a Roman number between M and C:
In Roman numbers you have
M = 1000
D = 500
C = 100
L = 50
X = 10
V = 5
I = 1


c.push("C") this can’t work, push() is an array method, c is a string.

You are also missing code to deal with 900, which in Roman numbers is “CM”

return console.log(newrom) will not work. console.log() is sort of like a return statement in itself, and this return statement is returning undefined. You will need to return directly the variable on a different line than the one you are printing it in the console.

These are a few things, maybe there is something else, at the moment I can’t do debugging more than this…

1 Like

1.I used only M for thousands because when i typed 9980 in converter from task link Roman Numerals i got MMMMMMMMMCMLXXX but it is not main point.
2.I don’t get why my switch is not working and when i turned c into array it didn’t work anyway.
3.I missed another parts of my code because they’re not working of the same misty reason.
4.I use console log for testing and i use chrome dev too.

The switch statement is inside an if else statement that is going to execute only if rom.length > 3 is false. So never with numbers above 999

For the return statement… if you leave your code like that, with return console.log(newrom), convertToRoman(9123) is undefined. You need to write return newrom for your function to actually return a value

Thanks, i got it, leahleen! I had to wrap case number into quotes and got rid off if-else. Solution is quite long but it is my first try


function convertToRoman(num) {
    let rom = num.toString().split("");
    let newrom = [];
    let i = 0;
    if (rom.length > 3) {
      while (i < rom[rom.length-4]) {
        newrom.push("M");
        i++;
      }
    }
    
      switch (rom[rom.length-3]) {
        case "1": newrom.push("C");
        break;
        case "2": newrom.push("CC");
        break;
        case "3": newrom.push("CCC");
        break;
        case "4": newrom.push("CD");
        break;
        case "5": newrom.push("D");
        break;
        case "6": newrom.push("DC");
        break;
        case "7": newrom.push("DCC");
        break;
        case "8": newrom.push("DCCC");
        break;
        case "9": newrom.push("CM");
        break;
                
      
    }
    
      switch (rom[rom.length-2]) {
        case "1": newrom.push("X");
        break;
        case "2": newrom.push("XX");
        break;
        case "3": newrom.push("XXX");
        break;
        case "4": newrom.push("XL");
        break;
        case "5": newrom.push("L");
        break;
        case "6": newrom.push("LX");
        break;
        case "7": newrom.push("LXX");
        break;
        case "8": newrom.push("LXXX");
        break;
        case "9": newrom.push("XC");
        break;
                
      
    }
 switch (rom[rom.length-1]) {
   case "1": newrom.push("I");
   break;
   case "2": newrom.push("II");
   break;
   case "3": newrom.push("III");
   break;
   case "4": newrom.push("IV");
   break;
   case "5": newrom.push("V");
   break;
   case "6": newrom.push("VI");
   break;
   case "7": newrom.push("VII");
   break;
   case "8": newrom.push("VIII");
   break;
   case "9": newrom.push("IX");
   break;
   case "10": newrom.push("X");
 }
 return newrom.join("");
}

convertToRoman(1006);