Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class RomanNumeralGenerator {
- constructor(number = null){
- this.state = {
- romanNumericalMapping: {
- units: {
- 1: 'I',
- 2: 'II',
- 3: 'III',
- 4: 'IV',
- 5: 'V',
- 6: 'VI',
- 7: 'VII',
- 8: 'VIII',
- 9: 'IX',
- },
- tens: {
- 10: 'X',
- 20: 'XX',
- 30: 'XXX',
- 40: 'XL',
- 50: 'L',
- 60: 'LX',
- 70: 'LXX',
- 80: 'LXXX',
- 90: 'XC',
- },
- hundreds: {
- 100: 'C',
- 200: 'CC',
- 300: 'CCC',
- 400: 'CD',
- 500: 'D',
- 600: 'DC',
- 700: 'DCC',
- 800: 'DCCC',
- 900: 'CM',
- },
- thousands:{
- 1000: 'M',
- 2000: 'MM',
- 3000: 'MMM',
- 4000: 'MMMM'
- }
- },
- inputNumber: number,
- outputNumber:''
- }
- if(this.state.inputNumber >= 1 && this.state.inputNumber <= 3999){
- this.state.outputNumber = this.generate(this.state.inputNumber);
- return console.log(this.state.outputNumber);
- }
- return 0;
- }
- generate(num){
- if(!Number.isInteger(num)){
- return 'Generate Function only accepts an integer value';
- }
- let numsArray = num.toString().split('');
- if(numsArray.length === 4){
- numsArray.map( (a,b) => {
- if(a > 0){
- if(b === 0) this.state.outputNumber = this.state.outputNumber.concat('', this.state.romanNumericalMapping.thousands[parseInt(a.concat('000'))]);
- else if(b === 1) this.state.outputNumber = this.state.outputNumber.concat('', this.state.romanNumericalMapping.hundreds[parseInt(a.concat('00'))]);
- else if(b === 2) this.state.outputNumber = this.state.outputNumber.concat('', this.state.romanNumericalMapping.tens[parseInt(a.concat('0'))]);
- else if(b === 3) this.state.outputNumber = this.state.outputNumber.concat('', this.state.romanNumericalMapping.units[parseInt(a)]);
- }
- });
- }else if(numsArray.length === 3){
- numsArray.map( (a,b) => {
- if(a > 0){
- if(b === 0) this.state.outputNumber = this.state.outputNumber.concat('', this.state.romanNumericalMapping.hundreds[parseInt(a.concat('00'))]);
- else if(b === 1) this.state.outputNumber = this.state.outputNumber.concat('', this.state.romanNumericalMapping.tens[parseInt(a.concat('0'))]);
- else if(b === 2) this.state.outputNumber = this.state.outputNumber.concat('', this.state.romanNumericalMapping.units[parseInt(a)]);
- }
- });
- }else if(numsArray.length === 2){
- numsArray.map( (a,b) => {
- if(a > 0){
- if(b === 0) this.state.outputNumber = this.state.outputNumber.concat('', this.state.romanNumericalMapping.tens[parseInt(a.concat('0'))]);
- else if(b === 1) this.state.outputNumber = this.state.outputNumber.concat('', this.state.romanNumericalMapping.units[parseInt(a)]);
- }
- });
- }else if(numsArray.length === 1){
- this.state.outputNumber = this.state.outputNumber.concat('', this.state.romanNumericalMapping.units[parseInt(numsArray[0])]);
- }
- return this.state.outputNumber;
- }
- }
- //tests
- t1 = new RomanNumeralGenerator(1);
- t2 = new RomanNumeralGenerator(5);
- t3 = new RomanNumeralGenerator(10);
- t4 = new RomanNumeralGenerator(20);
- t5 = new RomanNumeralGenerator(3999);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement