Roman Numeral Converter--将给定的数字转换为罗马数字

算法实现功能是:将给定的数字转换成罗马数字,所有返回的 罗马数字 都应该是大写形式。

参考的资源有:Roman Numerals Array.splice() ; Array.indexOf() Array.join();

function convert(num) {
 var nums = [1000,900,500,400,100,90,50,40,10,9,5,4,1];
  var romans =["m","cm","d","cd","c","xc","l","xl","x","ix","v","iv","i"];
  var str = '';
  nums.forEach(function(item,index,array){
    while(num >= item){
      str += romans[index];
      num -= item;
    }
  });
  
 return str.toUpperCase();
}

convert(36);


你可能感兴趣的:(JavaScript)