Quick actions

cmd+k|ctrl+k

Navigation

Languages

Hash Table Roman Numerals

Snippet info

Language

JavaScript

Visibility

public

Author

chaosrock

Created

2018-06-29T05:06:31Z

Updated

2018-06-29T05:07:45Z

class IntToRomanNumeral {
    constructor() {
        this.map = new Map ();
        this.map.set(1000, 'M');
        this.map.set(900, 'CM');
        this.map.set(500, 'D');
        this.map.set(400, 'CD');
        this.map.set(100, 'C');
        this.map.set(99, 'XC');
        this.map.set(50, 'L');
        this.map.set(40, 'XL');
        this.map.set(10, 'X');
        this.map.set(9, 'IX');
        this.map.set(8, 'VIII');
        this.map.set(7, 'VII');
        this.map.set(6, 'VI');
        this.map.set(5, 'V');
        this.map.set(4, 'IV');
        this.map.set(1, 'I');
    }
    
    convertNum(int) {
        let str = '';
        for (let [key, value] of this.map.entries()) {
            while (int >= key) {
                str += value;
                int -= key;
            }
        }
        return str;
    }
}

let test = new IntToRomanNumeral();
let value = test.convertNum(111);
console.log(value)
INFO