Skip to content

Instantly share code, notes, and snippets.

@caglarorhan
Created July 27, 2022 20:10
Show Gist options
  • Save caglarorhan/3ba9abe4309040997ce02f9f7df4cc5f to your computer and use it in GitHub Desktop.
Save caglarorhan/3ba9abe4309040997ce02f9f7df4cc5f to your computer and use it in GitHub Desktop.
Convert integers (<4000) to Roman
var intToRoman = function(num) {
let romans={
"1": ["I","V"],
"2": ["X","L"],
"3": ["C","D"],
"4": ["M","M"]
}
let s = num.toString();
let converted="";
for(let i=0 ; i<s.length; i++){
let numb= parseInt(s[i]);
let pos = s.length-i;
//console.log("Pozisyon:",pos, " sayi:", s[i]);
if(numb<4){
converted+=romans[pos][0].repeat(numb);
}else if(numb===4){
converted+=romans[pos][0]+romans[pos][1];
}else if(numb===5){
converted+=romans[pos][1];
}else if(numb<9){
converted+=romans[pos][1]+romans[pos][0].repeat(numb%5);
}else if(numb===9){
converted+=romans[pos][0]+romans[pos+1][0];
}
}
return converted;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment