Last active
April 1, 2022 10:15
-
-
Save kostikovmu/0c62ef986ebb8cb8e1e60ed97ebd5501 to your computer and use it in GitHub Desktop.
convert to roman js ( конвектор в римские числа )
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
function convertToRoman(num) { | |
const arr = num.toString().split('').map( el => +el).reverse(); | |
let res = '' | |
const v10 = [ | |
'I', 'X', 'C', 'M' | |
] | |
const v5 = [ | |
'V', 'L' , 'D' | |
] | |
arr.forEach( (el, i) => { | |
if(!el) return | |
if( el < 4 ) { | |
for( let j = 0; j < el; j++) { | |
res = v10[i] + res | |
} | |
} | |
if ( el === 4) { | |
res = v10[i] + v5[i] + res | |
} | |
if ( el > 4 && el < 9) { | |
let temp = v5[i] | |
for( let j = 0; j < el - 5 ; j++) { | |
temp += v10[i] | |
} | |
res = temp + res | |
} | |
if ( el === 9) { | |
res = v10[i] + v10[i+1] + res | |
} | |
}) | |
return res | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment