Roman Numeral Converter doesn't work!

Hi everyone! I’m tring to create the roman numeral converter. i wrote this code but there is a problem: the program works but i don’t pass the “exam”. Some one can hepls me?

function convertToRoman(num) {

function letM() {

  let m = Math.floor(num / 1000) % 10; 

  switch (m) {

     case 1:

     return ('M'); 

     case 2:

     return('MM');   

     case 3:

     return ('MMM');  

     };

}

function letC(){

  let c = Math.floor(num / 100) % 10;

  switch (c) {

     case 0:

     return('0');

     case 1:

     return('C');  

     case 2:

     return('CC');  

     case 3:

     return('CCC'); 

     case 4:

     return('CD');

     case 5:

     return('D');

     case 6:

     return('DC');

     case 7:

     return('DCC');

     case 8:

     return('DCCC');

     case 9:

     return('CM');

     }; 

}

function letD(){

  let d = Math.floor(num / 10) % 10;    

  switch (d) {

     case 0:

     return('0');

     case 1:

     return('X');

     case 2:

     return('XX');

     case 3:

     return('XXX');

     case 4:

     return('XL');

     case 5:

     return('L');

     case 6:

     return('LX');

     case 7:

     return('LXX');

     case 8:

     return('LXXX');

     case 9:

     return('XC');

     };

  }

function letU(){

  let u = num%10;   

  switch (u) {

     case 0:

     return('0');

     case 1:

     return('I');

     case 2:

     return('II');

     case 3:

     return('III');

     case 4:

     return('IV');

     case 5:

     return('V');

     case 6:

     return('VI');

     case 7:

     return('VII');

     case 8:

     return('VIII');

     case 9:

     return('IX');

     };

 }

var roman = (`${letM()}${letC()}${letD()}${letU()}`);

roman = roman.replace(/^undefined+/, "");

roman = roman.replace(/^undefined+/, "");

roman = roman.replace(/^undefined+/, "");

roman = roman.replace(/0+/, ""); 

roman = roman.replace(/0+/, ""); 

roman = roman.replace(/0+/, ""); 

 console.log(roman);

 };

 

convertToRoman(36);

So first, that is quite some lengthy code and second why replace undefined if you could just have the function return an empty string? Also I don’t understand where you get “0” from to replace…

Anyway, please provide a link to the challenge and also give us some info on what errors you are getting or what tests you are not passing.
Though I just figured, your convertToRoman() function itself doesn’t have a return value. So that could be it.

2 Likes

Seems to me like you don’t return the value. :smiley:

Edit: so as Jagaya said you should use a default case where you just return an empty string. And eliminate the case 0. But your approach also works so chers!

3 Likes

Thank guys! It happen because i copyed the wrong code from vs on my pc :smiling_face_with_tear:
Anyways i will try to improve my code! :smiley:

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

1 Like

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