lundi 5 juin 2017

Trouble displaying code on screen

This might sound like a simple thing for you programmers out there but i can't seem to figure it out. I'm making a program that converts Decimal Numbers into Roman Numerals. I've got the core code working but i can't seem to think how i can display the results on the screen. I would like it so that when the user types a number into a text box the result appears in another text box at the press of a button. Thanks for your time & help.

function convertToRoman(num) {
var romans = {
    M:  1000,
    CM: 900,
    D:  500,
    CD: 400,
    C:  100,
    XC: 90,
    L:  50,
    XL: 40,
    X:  10,
    IX: 9,
    V:  5,
    IV: 4,
    I:  1,
};

var result = '';

for (var key in romans) {
    if (num >= romans[key]) {
        result += key.repeat(Math.trunc(num / romans[key]));
        num -= romans[key] * Math.trunc(num / romans[key]);
    }
}

return result;

}

Aucun commentaire:

Enregistrer un commentaire