Created
July 27, 2022 20:10
-
-
Save caglarorhan/3ba9abe4309040997ce02f9f7df4cc5f to your computer and use it in GitHub Desktop.
Convert integers (<4000) to Roman
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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