Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

integer to roman javascript

function convertToRoman(num) {
  var roman = {
    M: 1000,
    CM: 900,
    D: 500,
    CD: 400,
    C: 100,
    XC: 90,
    L: 50,
    XL: 40,
    X: 10,
    IX: 9,
    V: 5,
    IV: 4,
    I: 1
  };
  var str = '';

  for (var i of Object.keys(roman)) {
    var q = Math.floor(num / roman[i]);
    num -= q * roman[i];
    str += i.repeat(q);
  }

  return str;
}
Comment

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 :: monaco editor get value 
Javascript :: creatable select react 
Javascript :: angular scroll to element horizontally 
Javascript :: Create JavaScript Strings 
Javascript :: how to create request body javascript 
Javascript :: random number generatoe js 
Javascript :: form validation for email in js 
Javascript :: jquery add 
Javascript :: how to install exact package lock version in package-lock.json 
Javascript :: css select all links in div 
Javascript :: load.json 
Javascript :: JavaScript block-scoped Variable9 
Javascript :: react materialize cdn 
Javascript :: array with object same keys 
Javascript :: mean stack 
Javascript :: google autocomplete not returning lat long 
Javascript :: crypto in node js 
Javascript :: javascript good practice 
Javascript :: target child event 
Javascript :: check last url in javascript 
Javascript :: how to use buffer in angular by using browserify 
Javascript :: how to add objects in array 
Javascript :: react native app crashing on start 
Javascript :: js promise 
Javascript :: export socket io connection 
Javascript :: nodejs global 
Javascript :: sequelize one to many 
Javascript :: how to give custom name to collection in mongoose 
Javascript :: object assign 
Javascript :: enzyme testing 
ADD CONTENT
Topic
Content
Source link
Name
7+5 =