Ver Mensaje Individual
  #1 (permalink)  
Antiguo 31/03/2009, 16:13
worldPeace_Plis
Usuario no validado
 
Fecha de Ingreso: marzo-2007
Mensajes: 238
Antigüedad: 17 años, 2 meses
Puntos: 5
Comandos de string(2)

hola tunait, no se como hacer esto, mira es una pruebita parasaber si es stringo es number al enviar info rmacion de un formulario, luego va a la funcion de js, entra a un switch case donde case hay tres opciones 1, 2 y default
Código javascript:
Ver original
  1. function ej_6(param)
  2. {
  3. var wvar = param;
  4. // alert ('esto vale  wvar (myvar): '+wvar+' ('+parseInt(wvar)+'), y esto param:'+param+' ('+parseInt(param)+')');
  5. // var a = 2;
  6.  
  7. miValor=parseInt(param);
  8. alert('miValor: '+miValor+', '+typeof miValor);
  9.  
  10. switch (wvar)
  11. {
  12.  
  13. case 1:
  14. try {
  15. var resultado = 5/a;
  16. } catch(excepcion) {
  17. alert(excepcion);
  18. }break;
  19.  
  20. case 2:
  21. try {
  22. if(typeof a == "undefined" || isNaN(a)) {
  23. throw new Error('La variable "a" no es un número');
  24. }
  25. var resultado = 5/a;
  26. } catch(excepcion) {
  27. alert(excepcion);
  28. } finally {
  29. alert("Se ejecuta");
  30. }
  31. break;
  32.  
  33. default:
  34.    try {
  35.    if(typeof wvar == 'undefined') {
  36.    wvar = 'implementa';
  37.    alert('wvar ahora es '+wvar); // el objeto posee la propiedad buscada
  38.    }
  39.    /*if (wvar != 'implementa') {
  40.    throw new Error('"wvar" no es un parametro valido de la funcion');
  41.    }*/
  42.    **//ejemplo de línea con error
  43. ** writte("Esta linea probocará una excepción.");**
  44.    
  45.    alert('Este es el ejercicio 6, busca '+wvar);
  46.    var elObjeto = new Object({
  47.         unaProp:5,
  48.         otraProp:'estilos',
  49.         implementa:function() { sentencias; }
  50.         });
  51.    } catch (exception) {
  52. **    if (exception.description == null)
  53.       {
  54.       alert("Excepción: " + exception.message);**
  55.       } else {
  56.       alert("Excepción: " + exception.description);
  57.       }
  58.    } finally
  59.    {
  60.    if(elObjeto instanceof Array)
  61.    alert("finally, Es un array");
  62.    else if(elObjeto instanceof Object)
  63.    alert("finally, Es un objeto");
  64.    }
  65.  
  66. } // end switch
  67. }
  68. // -->
  69. </script>
Código HTML:
<center>
<form>
<div style="width:60%; text-align:center; margin:0px auto; border:#CCCC99 solid 1px; padding:4px;">
<input type="button" onclick="javascript:ej_6(1)" value="try catch, ej 1" />
&nbsp;&nbsp;
<input type="button" onclick="javascript:ej_6(2)" value="try catch, ej 2" />
&nbsp;&nbsp;
<input type="button" onclick="javascript:ej_6(this.form.myvar)" value="ejercicio 6" />
&nbsp;
<input type="text" size="20" name="myvar" /><br />
<input type="reset" />
</div>
</form>
</center> 
Como consigo saber cuando viene un numero o un string, no se como! :(