I need to change this java program to a javascript program please. I dont even k
ID: 645581 • Letter: I
Question
I need to change this java program to a javascript program please. I dont even know where to begin
public class RomanNumerals1 {
static String [] getStrokes(int orderOfMagnitude) {
String [] array = new String[3];
switch (orderOfMagnitude) {
case 1:
String [] array1 = {"I","V","X"};
array = array1;
break;
case 10:
String [] array10 = {"X","L","C"};
array = array10;
break;
case 100:
String [] array100 = {"C","D","M"};
array = array100;
break;
case 1000:
String [] array1000 = {"M","?","?"};
array = array1000;
break;
}
return array;
}
static String convertDigit(int remainder, int divisor) {
String [] numerals = getStrokes(divisor);
String low = numerals[0];
String mid = numerals[1];
String high = numerals[2];
String value = "";
switch (remainder) {
case 0: break;
case 1:
case 2:
case 3:
for (int i=0; i<remainder; i++) {
value += low;
}
break;
case 4:
value = low + mid;
break;
case 5:
value = mid;
break;
case 6:
case 7:
case 8:
value = mid;
for (int i=0; i<(remainder-5); i++) {
value+= low;
}
break;
case 9:
value = low + high;
break;
case 10:
value = high;
break;
}
return value;
}
static String convertNumber(int n) {
//System.out.printf("Number %d: ", n);
int divisor = 1000;
String value = "";
while (n > 0) {
int quotient = n / divisor;
value += convertDigit(quotient, divisor);
n = n - quotient * divisor;
divisor = divisor / 10;
}
return value;
}
public static void main(String [] args) {
System.out.printf("%d: %s ", 3854, convertNumber(3854));
System.out.printf("%d: %s ", 2, convertNumber( 2));
}
}
Explanation / Answer
Number.prototype.toRoman= function(){ var num= Math.floor(this), val, s= '', i= 0, v= [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1], r= ['M', 'CM', 'D', 'CD', 'C', 'XC', 'L', 'XL', 'X', 'IX', 'V', 'IV', 'I'], toBigRoman= function(n){ var ret= '', n1= '', rem= n; while(rem>1000){ var prefix= '', suffix= '', n= rem, s= ''+rem; while(n>1000){ n/= 1000; prefix+= '('; suffix+= ')'; } n1= Math.floor(n); rem= +(s.replace((''+n1), '')); ret+= prefix+ n1.toRoman()+suffix; } return ret+ rem.toRoman(); } if(this-num || num3999) return toBigRoman(num); while(num){ val= v[i]; while(num>= val){ num-= val; s+= r[i]; } ++i; } return s; }Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.