Skip to content

Instantly share code, notes, and snippets.

@kostikovmu
Last active April 1, 2022 10:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kostikovmu/0c62ef986ebb8cb8e1e60ed97ebd5501 to your computer and use it in GitHub Desktop.
Save kostikovmu/0c62ef986ebb8cb8e1e60ed97ebd5501 to your computer and use it in GitHub Desktop.
convert to roman js ( конвектор в римские числа )
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