Ver Mensaje Individual
  #1 (permalink)  
Antiguo 24/02/2011, 03:49
xai1985
 
Fecha de Ingreso: octubre-2010
Mensajes: 26
Antigüedad: 13 años, 7 meses
Puntos: 1
Consejo con switch please!

Estab empollando un poco el javascript y cree un programa que pide que introduzcas tres numeros y luego te los ordena de menor a mayor:


<html>
<head>
<title>Problema</title>
</head>
<body>

<script language="javascript">
function menor(x1,x2,x3){
if (x1<x2 && x2<x3){
document.write(x1 + " " + x2 + " " + x3)}
if (x2<x3 && x3<x1){
document.write(x2 + ' ' + x3 + ' ' + x1)}
if (x2<x1 && x1<x3){
document.write(x2 + ' ' + x1 + ' ' + x3)}
if (x3<x2 && x2<x1){
document.write(x3 + ' ' + x2 + ' ' + x1)}
if (x1<x3 && x3<x2){
document.write(x1 + ' ' + x3 + ' ' + x2)}
if (x3<x1 && x1<x2){
document.write(x3 + ' ' + x1 + ' ' + x2)}
}
x1=prompt("Introduce un número:")
x1=parseInt(x1)
x2=prompt("Introduce un número:")
x2=parseInt(x2)
x3=prompt("Introduce un número:")
x3=parseInt(x3)
menor(x1,x2,x3)
</script>

</body>
</html>


Weno el parseInt sobra :P ejeje, da igual, mi pregunta es, si quiero convertir los if en un switch, como lo hago??
Yo lo he montado así, me pide los numeros, pero luego no me los recoge y ordena, no muestra nada... Alguna mente superior me puede ayudar? thanks


<html>
<head>
<title>Problema</title>
</head>
<body>

<script language="javascript">
function menor(x1,x2,x3){
switch {
case x1<x2 && x2<x3:
document.write(x1 + " " + x2 + " " + x3)
break
case x2<x3 && x3<x1:
document.write(x2 + ' ' + x3 + ' ' + x1)
break
case x2<x1 && x1<x3:
document.write(x2 + ' ' + x1 + ' ' + x3)
break
case x3<x2 && x2<x1:
document.write(x3 + ' ' + x2 + ' ' + x1)
break
case x1<x3 && x3<x2:
document.write(x1 + ' ' + x3 + ' ' + x2)
break
case x3<x1 && x1<x2:
document.write(x3 + ' ' + x1 + ' ' + x2)
break
}
}
x1=prompt("Introduce un número:")
x1=parseInt(x1)
x2=prompt("Introduce un número:")
x2=parseInt(x2)
x3=prompt("Introduce un número:")
x3=parseInt(x3)
menor(x1,x2,x3)
</script>

</body>
</html>