“Comment convertir Roman en décimal en javascript” Réponses codées

comment convertir la décimale en romain en 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;
  };
mrmalik610

Comment convertir Roman en décimal en 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();
};
mrmalik610

Réponses similaires à “Comment convertir Roman en décimal en javascript”

Questions similaires à “Comment convertir Roman en décimal en javascript”

Plus de réponses similaires à “Comment convertir Roman en décimal en javascript” dans JavaScript

Parcourir les réponses de code populaires par langue

Parcourir d'autres langages de code