Translate

Number to Roman Numeral Converter

("Please enter the number to convert from number to roman numeral up to 255."

answer

1 comment :

  1. This is the code:
    function convert(n) {
    var romanN = "";
    var hundreds = (n / 100 | 0);
    var fifties = ((n % 100) / 50 | 0);
    var tens = (((n % 100) % 50) / 10 | 0);
    var fives = ((((n % 100) % 50) % 10) / 5 | 0);
    var ones = (((n % 100) % 50) % 10) % 5;
    if (n < 256) {
    for (var i = 0; i < hundreds; i++) {
    romanN = romanN + "C";
    }
    if (n % 100 >= 90 && fifties !== 0) {
    romanN = romanN + "XC";
    fifties--;
    tens = tens - 4;
    }
    for (var i = 0; i < fifties; i++) {
    romanN = romanN + "L";
    }
    if (n % 50 >= 40 && tens !== 0) {
    romanN = romanN + "XL";
    tens = tens - 4;
    }
    for (var i = 0; i < tens; i++) {
    romanN = romanN + "X";
    }
    if (ones % 4 === 0 && fives % 2 === 1 && ones !== 0 && fives !== 0) {
    romanN = romanN + "IX";
    fives = 0;
    ones = 0;
    }
    for (var i = 0; i < fives; i++) {
    romanN = romanN + "V";
    }
    if (ones % 4 === 0 && ones !== 0) {
    //if (fives % 2 === 0 && fives !== 0) {
    romanN = romanN + "IV";
    //}
    }
    else {
    for (var i = 0; i < ones; i++) {
    romanN = romanN + "I";
    }
    }
    }
    else {
    romanN = "NA";
    }
    return romanN;
    }

    ReplyDelete