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

PREVIOUS NEXT
Code Example
Javascript :: Get List of all files in a directory in Node.js 
Javascript :: react props.children proptype 
Javascript :: error:03000086:digital envelope routines::initialization error 
Javascript :: display none js 
Javascript :: get height element use js 
Javascript :: check if date time string is invalid date js 
Javascript :: jquery disable class attribute 
Javascript :: how to check if a string has only alphabets in javascript 
Javascript :: validate Alphabet Letter js 
Javascript :: javascript get attribute 
Javascript :: Setting a background Image With React Inline Styles 
Javascript :: localsstorage array append element 
Javascript :: how to run vue js project on different port 
Javascript :: vuejs v-for reverse 
Javascript :: function that return shortest of words in the given array js 
Javascript :: jquery click or touch 
Javascript :: javascript reverse a string 
Javascript :: js explode equivalent 
Javascript :: filter array with unique objects javascript 
Javascript :: electron hide top bar 
Javascript :: cypress set window size 
Javascript :: english number to bangla javascript 
Javascript :: append before jquery 
Javascript :: × MUI: makeStyles is not longer exported from @mui/material/styles. You have to import it from @mui/styles. 
Javascript :: js string limit length 
Javascript :: jquery add class 
Javascript :: convert da decimale a hex javascript 
Javascript :: uuid generator pure javascript 
Javascript :: on click fade out jquery 
Javascript :: javascript promise sleep 
ADD CONTENT
Topic
Content
Source link
Name
7+5 =