Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Convert a number into Roman numeral
// Derived from https://stackoverflow.com/questions/9083037/convert-a-number-into-a-roman-numeral-in-javascript#37723879
import { List } from 'immutable'
const romanMatrix = List.of(
List.of(1000, 'M'),
List.of(900, 'CM'),
List.of(500, 'D'),
List.of(400, 'CD'),
List.of(100, 'C'),
List.of(90, 'XC'),
List.of(50, 'L'),
List.of(40, 'XL'),
List.of(10, 'X'),
List.of(9, 'IX'),
List.of(5, 'V'),
List.of(4, 'IV'),
List.of(1, 'I')
)
function convertToRoman (number) {
return romanMatrix
.toSeq()
.filter(roman => number >= roman.first())
.map(roman => roman.last() + convertToRoman(number - roman.first()))
.first('')
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.