Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

how to convert decimal to roman in javascript

  const decimalToRoman = () => {
    const intToRoman = (num) => {
      let result = "";
      while (num) {
        if (num >= 1000) {
          result += "M";
          num -= 1000;
        } else if (num >= 500) {
          if (num >= 900) {
            result += "CM";
            num -= 900;
          } else {
            result += "D";
            num -= 500;
          }
        } else if (num >= 100) {
          if (num >= 400) {
            result += "CD";
            num -= 400;
          } else {
            result += "C";
            num -= 100;
          }
        } else if (num >= 50) {
          if (num >= 90) {
            result += "XC";
            num -= 90;
          } else {
            result += "L";
            num -= 50;
          }
        } else if (num >= 10) {
          if (num >= 40) {
            result += "XL";
            num -= 40;
          } else {
            result += "X";
            num -= 10;
          }
        } else if (num >= 5) {
          if (num >= 9) {
            result += "IX";
            num -= 9;
          } else {
            result += "V";
            num -= 5;
          }
        } else {
          if (num >= 4) {
            result += "IV";
            num -= 4;
          } else {
            result += "I";
            num -= 1;
          }
        }
      }
      return result;
    };
    const newText = intToRoman(Math.abs(Number(string)));
    return newText;
  };
Comment

how to convert roman to decimal in javascript

const romanToDecimal = () => {
  const romanToInt = (s) => {
    const legend = "IVXLCDM";
    const l = [1, 5, 10, 50, 100, 500, 1000];
    let sum = 0;
    while (s) {
      if (!!s[1] && legend.indexOf(s[0]) < legend.indexOf(s[1])) {
        sum += l[legend.indexOf(s[1])] - l[legend.indexOf(s[0])];
        s = s.substring(2, s.length);
      } else {
        sum += l[legend.indexOf(s[0])];
        s = s.substring(1, s.length);
      }
    }
    return sum;
  };
  return romanToInt(text.toUpperCase()).toString();
};
Comment

PREVIOUS NEXT
Code Example
Javascript :: search query in javascript 
Javascript :: Run FEnvQueryRequest 
Javascript :: oridnal suffix 
Javascript :: knockout subscribe 
Javascript :: javascript copy text by id to clipboard 
Javascript :: hex color js 
Javascript :: operadores de asignacion javascript 
Javascript :: JavaScript Change the Value of Variables 
Javascript :: Finding palindrome using for loop 
Javascript :: javascript prototype chaining 
Javascript :: javascript rest parameter 
Javascript :: javascript Skip Items 
Javascript :: javascript Undeclared objects are not allowed 
Javascript :: JavaScript Object Prototypes 
Javascript :: Källmappningsfel: Error: NetworkError when attempting to fetch resource. Resurs-URL: moz-extension://f820ec62-0644-495b-9cd6-fe7d01cdd955/browser-polyfill.js Källmappnings-URL: browser-polyfill.min.js.map 
Javascript :: nodejs: Basic: managing file: Read, Write, Create, Delete 
Javascript :: query middleware in express 
Javascript :: ngswitch example on string 
Javascript :: Elementor Hide Sticky Header on Scroll Down - Show on Scroll Up 
Javascript :: phaser create animation from texture atlas 
Javascript :: share.sharesingle facebook react native 
Javascript :: npm deploy next js with tailwind 
Javascript :: show json preformatted 
Javascript :: smembers in redis 
Javascript :: javascript setinterval run immediately 
Javascript :: js promise example 
Javascript :: sort array method 
Javascript :: what is closures in javascript 
Javascript :: material-ui add icon to switch when on 
Javascript :: remove duplicates array javascript 
ADD CONTENT
Topic
Content
Source link
Name
7+3 =