将整数转换为罗马数字 - 一定有更好的方法

Convert integer to roman numerals - there must be a better way

我正在学习 FreeCodeCamp 课程中的中级算法。其中之一涉及将整数转换为罗马数字。我的解决方案(如下所示)有效,但如果您愿意的话,它非常接近 "naive" 方法。该任务提示应该使用 array.splice()、array.indexOf() 和 array.join()。我的实现只使用 array.join()。

经过编辑的精确问题:有人会提供一个使用上述方法的所有的实现吗?

谢谢。

我的实现:

function convertToRoman(num) {
  var I = 1, V = 5, X = 10, L = 50, C = 100, D = 500, M = 1000;
  var numerals = [];
  var i;

  if(num >= 1000){
    var numMs =  Math.floor(num / 1000);
    for (i = 0; i < numMs; i++){
      numerals.push('M');
    }
    num = num % 1000; 
  }

  if(num >= 900){
    numerals.push('CM');
    num = num - 900;
  }

  if(num < 900){
    if(num >= 500){
      numerals.push('D');
      num = num - 500;
    }
    if(num >= 400){
      numerals.push('CD');
      num = num - 400;
    }
    var numCs = Math.floor(num / 100);
    for(i = 0; i < numCs; i++){
      numerals.push('C');
    }
    num = num % 100;
  }

  if(num >= 90){
    numerals.push('XC');
    num = num - 90;
  }

  if(num < 90){
    if(num >= 50){
      numerals.push('L');
      num = num - 50;
    }
    if(num >= 40){
      numerals.push('XL');
      num = num - 40;
    }
    var numXs = Math.floor(num / 10);
    for(i = 0; i < numXs; i++){
      numerals.push('X');
    }
    num = num % 10;
  }

  if(num == 9){
    numerals.push('IX');
    num = num - 9;
  } 

  if(num < 9){
    if(num >= 5){
      numerals.push('V');
      num = num - 5;
    }
    if(num >=4){
      numerals.push('IV');
      num = num - 4;
    }
    var numIs = Math.floor(num / 1);
    for(i = 0; i < numIs; i++){
      numerals.push('I');
    }
  }

  var converted = numerals.join('');

  return converted;
} 

更新:找到另一个答案 here,但它没有使用我感兴趣的方法。

您可以进行以下操作;

function toRoman(n){
  var numerals = ["I","V","X","L","C","D","M"];
  return n.toString()
          .split("")
          .reverse()
          .reduce((p,c,i) => (c === "0" ? ""
                                        : c < "4" ? numerals[2*i].repeat(c)
                                                  : c === "4" ? numerals[2*i] + numerals[2*i+1]
                                                              : c < "9" ? numerals[2*i+1] + numerals[2*i].repeat(c-5)
                                                                        : numerals[2*i] + numerals[2*i+2]) + p,"");
}
console.log(toRoman(1453));

这是一个使用 Array#reduce 来转换数字的任何部分的提案。

function toRoman(i) {
    return ('0000' + i).slice(-4).split('').map(Number).reduce(function (r, a, i) {
        var value = 'MDCLXVI';
        if (a === 4 || a === 9) {
            r += value[i * 2];
            a++;
        }
        if (a === 10) {
            r += value[i * 2 - 2];
            a -= 10;
        }
        if (a >= 5) {
            r += value[i * 2 - 1];
            a -= 5;
        }
        while (a) {
            r += value[i * 2];
            a--;
        }
        return r;
    }, '');
}

var i;
for (i = 1; i <= 3000; i++) {
    document.getElementById('tt').innerHTML += i + ' ' + toRoman(i) + `\n`;
}
<pre id="tt"></pre>