289、将给定的数字转换成罗马数字。所有返回的罗马数字都应该是大写形式。
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();
}
function convert(num){
varalpha=['I','V','X','L','C','D','M'],roman="",bit=0;
while(num>0)
{
var tempnum = num%10;
switch(tempnum)
{
case3:{roman=alpha[bit]+roman;tempnum--;}
case2:{roman=alpha[bit]+roman;tempnum--;}
case1:{roman=alpha[bit]+roman;break;}
case4:{roman=alpha[bit+1]+roman;roman=alpha[bit]+roman;break;}
case8:{roman=alpha[bit]+roman;tempnum--;}
case7:{roman=alpha[bit]+roman;tempnum--;}
case6:{roman=alpha[bit]+roman;tempnum--;}
case5:{roman=alpha[bit+1]+roman;break;}
case9:{roman=alpha[bit+2]+roman;roman=alpha[bit]+roman;break;}
default:{break;}}
bit+=2;num=Math.floor(num /10);
return roman;
}