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 originalfunction ej_6(param)
{
var wvar = param;
// alert ('esto vale wvar (myvar): '+wvar+' ('+parseInt(wvar)+'), y esto param:'+param+' ('+parseInt(param)+')');
// var a = 2;
miValor=parseInt(param);
alert('miValor: '+miValor+', '+typeof miValor);
switch (wvar)
{
case 1:
try {
var resultado = 5/a;
} catch(excepcion) {
alert(excepcion);
}break;
case 2:
try {
if(typeof a == "undefined" || isNaN(a)) {
throw new Error('La variable "a" no es un número');
}
var resultado = 5/a;
} catch(excepcion) {
alert(excepcion);
} finally {
alert("Se ejecuta");
}
break;
default:
try {
if(typeof wvar == 'undefined') {
wvar = 'implementa';
alert('wvar ahora es '+wvar); // el objeto posee la propiedad buscada
}
/*if (wvar != 'implementa') {
throw new Error('"wvar" no es un parametro valido de la funcion');
}*/
**//ejemplo de línea con error
** writte("Esta linea probocará una excepción.");**
alert('Este es el ejercicio 6, busca '+wvar);
var elObjeto = new Object({
unaProp:5,
otraProp:'estilos',
implementa:function() { sentencias; }
});
} catch (exception) {
** if (exception.description == null)
{
alert("Excepción: " + exception.message);**
} else {
alert("Excepción: " + exception.description);
}
} finally
{
if(elObjeto instanceof Array)
alert("finally, Es un array");
else if(elObjeto instanceof Object)
alert("finally, Es un objeto");
}
} // end switch
}
// -->
</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" />
<input type="button" onclick="javascript:ej_6(2)" value="try catch, ej 2" />
<input type="button" onclick="javascript:ej_6(this.form.myvar)" value="ejercicio 6" />
<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! :(