<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html>
<head>
<title>TODO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script>
function convertir() {
var numero = "" + document.getElementById('numero').value;
var str = "";
var op = "";
for (var i = 0; i < numero.length; i++) {
var car = numero.charAt(i);
if (numero.length == 4) {
if (i === 0) {
str += millar(car);
}
if (i === 1) {
str += centetimo(car);
}
if (i === 2) {
str += decimo(car);
}
if (i === 3) {
str += unidad(car);
}
}
if (numero.length == 3) {
if (i === 0) {
str += centetimo(car);
}
if (i === 1) {
str += decimo(car);
}
if (i === 2) {
str += unidad(car);
}
}
if (numero.length == 2) {
if (i === 0) {
str += decimo(car);
}
if (i === 1) {
str += unidad(car);
}
}
if (numero.length == 1) {
if (i === 0) {
str += unidad(car);
}
}
}
document.getElementById('resultado').value = str;
}
function millar(car) {
switch (car) {
case '1':
return "M";
break;
case '2':
return "MM";
break;
case '3':
return "MMM";
break;
default:
return "";
break;
}
}
function centetimo(car) {
switch (car) {
case '1':
return "C";
break;
case '2':
return "CC";
break;
case '3':
return "CCC";
break;
case '4':
return "CD";
break;
case '5':
return "D";
break;
case '6':
return "DC";
break;
case '7':
return "DCC";
break;
case '8':
return "DCC";
break;
case '9':
return "CM";
break;
default:
return "";
break;
}
}
function decimo(car) {
switch (car) {
case '1':
return "X";
break;
case '2':
return "XX";
break;
case '3':
return "XXX";
break;
case '4':
return "XL";
break;
case '5':
return "L";
break;
case '6':
return "LX";
break;
case '7':
return "LXX";
break;
case '8':
return "LXXX";
break;
case '9':
return "XC";
break;
default:
return "";
break;
}
}
function unidad(car) {
switch (car) {
case '1':
return "I";
break;
case '2':
return "II";
break;
case '3':
return "III";
break;
case '4':
return "IV";
break;
case '5':
return "V";
break;
case '6':
return "VI";
break;
case '7':
return "VII";
break;
case '8':
return "VIII";
break;
case '9':
return "IX";
break;
default:
return "";
break;
}
}
</script>
</head>
<body>
Número: <input type="number" id="numero" min="0" max="3999"/><br>
<button onclick="convertir()">Convertir</button><br>
Número en romano: <input type="text" id="resultado">
</body>
</html>
No hay comentarios.:
Publicar un comentario