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

roman numeral converter + javascript

function convertToRoman(num) {
    var roman = '';
    var decimal = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1];
    var romanNum = ['M', 'CM', 'D', 'CD', 'C', 'XC', 'L', 'XL', 'X', 'IX', 'V', 'IV', 'I'];
    for (var i = 0; i < decimal.length; i++) {
        while (decimal[i] <= num) {
            roman += romanNum[i];
            num -= decimal[i];
        }
    }
    return roman;
}
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

PREVIOUS NEXT
Code Example
Javascript :: counter plus minus for quantity 
Javascript :: write "hello world" 
Javascript :: submit form on ctrl enter 
Javascript :: Plumsail set form lookup field value on form load 
Javascript :: how to put condition on pagination material table 
Javascript :: i in javascript 
Javascript :: if the params of usequery updated 
Javascript :: getderivedfromstate alternative 
Javascript :: filter by last week 
Javascript :: web3 returns an object promise instead of number 
Javascript :: parcel react 
Javascript :: How to escape specific JSON characters in Powershell 
Javascript :: Call Injected AngularJs Service In Controller From Blazor Within CustomElement/WebComponent 
Javascript :: ! function in javascript 
Javascript :: Popover AngularJs quickly disappearing 
Javascript :: How to get one items from my Firebase realtime Database with Angular Ionic 
Javascript :: EXPO useEffect not called on navigating to same screen 
Javascript :: How to make this code cleaner? react native 
Javascript :: python regex consecutive characters 
Javascript :: fireOnChange 
Javascript :: mongodb create index json 
Javascript :: react native push notifications cancel delivered notification 
Javascript :: react state based router 
Javascript :: socket io inside route express not working 
Javascript :: puppeteer create folder 
Javascript :: javascript quotes 
Javascript :: js get first elements of array 
Javascript :: JavaScript Using es6(ES2015) Destructuring assignment 
Javascript :: ahead-of-time (AOT) compilation 
Javascript :: NavBar with divs 
ADD CONTENT
Topic
Content
Source link
Name
2+1 =