Advertisement
Guest User

Untitled

a guest
Aug 21st, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.99 KB | None | 0 0
  1. class RomanNumeralGenerator {
  2.  
  3. constructor(number = null){
  4.  
  5. this.state = {
  6. romanNumericalMapping: {
  7. units: {
  8. 1: 'I',
  9. 2: 'II',
  10. 3: 'III',
  11. 4: 'IV',
  12. 5: 'V',
  13. 6: 'VI',
  14. 7: 'VII',
  15. 8: 'VIII',
  16. 9: 'IX',
  17. },
  18. tens: {
  19. 10: 'X',
  20. 20: 'XX',
  21. 30: 'XXX',
  22. 40: 'XL',
  23. 50: 'L',
  24. 60: 'LX',
  25. 70: 'LXX',
  26. 80: 'LXXX',
  27. 90: 'XC',
  28. },
  29. hundreds: {
  30. 100: 'C',
  31. 200: 'CC',
  32. 300: 'CCC',
  33. 400: 'CD',
  34. 500: 'D',
  35. 600: 'DC',
  36. 700: 'DCC',
  37. 800: 'DCCC',
  38. 900: 'CM',
  39. },
  40. thousands:{
  41. 1000: 'M',
  42. 2000: 'MM',
  43. 3000: 'MMM',
  44. 4000: 'MMMM'
  45. }
  46. },
  47. inputNumber: number,
  48. outputNumber:''
  49. }
  50.  
  51. if(this.state.inputNumber >= 1 && this.state.inputNumber <= 3999){
  52. this.state.outputNumber = this.generate(this.state.inputNumber);
  53. return console.log(this.state.outputNumber);
  54. }
  55.  
  56. return 0;
  57. }
  58.  
  59. generate(num){
  60. if(!Number.isInteger(num)){
  61. return 'Generate Function only accepts an integer value';
  62. }
  63. let numsArray = num.toString().split('');
  64. if(numsArray.length === 4){
  65. numsArray.map( (a,b) => {
  66. if(a > 0){
  67. if(b === 0) this.state.outputNumber = this.state.outputNumber.concat('', this.state.romanNumericalMapping.thousands[parseInt(a.concat('000'))]);
  68. else if(b === 1) this.state.outputNumber = this.state.outputNumber.concat('', this.state.romanNumericalMapping.hundreds[parseInt(a.concat('00'))]);
  69. else if(b === 2) this.state.outputNumber = this.state.outputNumber.concat('', this.state.romanNumericalMapping.tens[parseInt(a.concat('0'))]);
  70. else if(b === 3) this.state.outputNumber = this.state.outputNumber.concat('', this.state.romanNumericalMapping.units[parseInt(a)]);
  71. }
  72. });
  73. }else if(numsArray.length === 3){
  74. numsArray.map( (a,b) => {
  75. if(a > 0){
  76. if(b === 0) this.state.outputNumber = this.state.outputNumber.concat('', this.state.romanNumericalMapping.hundreds[parseInt(a.concat('00'))]);
  77. else if(b === 1) this.state.outputNumber = this.state.outputNumber.concat('', this.state.romanNumericalMapping.tens[parseInt(a.concat('0'))]);
  78. else if(b === 2) this.state.outputNumber = this.state.outputNumber.concat('', this.state.romanNumericalMapping.units[parseInt(a)]);
  79. }
  80. });
  81. }else if(numsArray.length === 2){
  82. numsArray.map( (a,b) => {
  83. if(a > 0){
  84. if(b === 0) this.state.outputNumber = this.state.outputNumber.concat('', this.state.romanNumericalMapping.tens[parseInt(a.concat('0'))]);
  85. else if(b === 1) this.state.outputNumber = this.state.outputNumber.concat('', this.state.romanNumericalMapping.units[parseInt(a)]);
  86. }
  87. });
  88. }else if(numsArray.length === 1){
  89. this.state.outputNumber = this.state.outputNumber.concat('', this.state.romanNumericalMapping.units[parseInt(numsArray[0])]);
  90. }
  91. return this.state.outputNumber;
  92. }
  93. }
  94.  
  95. //tests
  96. t1 = new RomanNumeralGenerator(1);
  97. t2 = new RomanNumeralGenerator(5);
  98. t3 = new RomanNumeralGenerator(10);
  99. t4 = new RomanNumeralGenerator(20);
  100. t5 = new RomanNumeralGenerator(3999);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement