Ver Mensaje Individual
  #11 (permalink)  
Antiguo 22/10/2011, 23:13
Avatar de Panino5001
Panino5001
Me alejo de Omelas
 
Fecha de Ingreso: mayo-2004
Ubicación: -34.637167,-58.462984
Mensajes: 5.148
Antigüedad: 19 años, 10 meses
Puntos: 834
Respuesta: Desafios Javascript, ronda 2

Estuve un poco complicado estos días, pero ahí va mi solución para el desafío de los romanos:
Código:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title></title>
<script type="text/javascript">
function arabigo2Romano(n){
	var _replace=['M','CM','D','CD','C','XC','L','XL','X','IX','V','IV','I'],
	_search=[1000,900,500,400,100,90,50,40,10,9,5,4,1],
	tmp='';
	for(var i=0,len=_replace.length;i<len;i++)
		while(n>=_search[i]){
			tmp+=_replace[i];
			n-=_search[i];
		}
	return tmp;
}
onload=function(){
	for(var i=0;i<100;i++){
		var n=Math.round(Math.random()*9999);
		document.getElementById('log').innerHTML+=n+': '+arabigo2Romano(n)+'<br>';	
	}
}
</script>
</head>

<body>
<div id="log"></div>
</body>
</html>
http://jsfiddle.net/panino/UuQ7m/

Última edición por Panino5001; 22/10/2011 a las 23:37