Tema: recursividad
Ver Mensaje Individual
  #3 (permalink)  
Antiguo 08/04/2013, 09:01
chamo_latoso18
 
Fecha de Ingreso: marzo-2013
Mensajes: 7
Antigüedad: 11 años, 1 mes
Puntos: 1
Respuesta: recursividad

la verda si eso de elevar x a la n y alo hice, el codigo esta bien, y lo revise varias vecea, y tambien me lo revisaron, pero al momento de validad los datos en mi pagina html, es que me lanza un error, la funcion me quedo asi:
function poter(x,n){
if (n!=0) return pote(pow(x,n-1))*x;
return 1;
}
por que solo se trata de numeros enteros positivos, y como te digo el problema esta en la validasion ,el codigo completo que hice es este :
<!DOCTYPE html>
<html>
<head>
<title>Ejemplo 4</title>
<script src="sis101.js"></script>
<script>
//Calculo recursivo del numero de digitos
function poter(x,n){
if (n!=0) return pote(pow(x,n-1))*x;
return 1;
}
//Validacion del numero y llamada a la funcion recursiva
function pote(x,n){
if (isNaN(n))
throw new Error("fact: Los datos deben ser numericos.");
if (frac(n)==0)
throw new Error("fact: El exponencial debe ser entero.");
if (n<0)
throw new Error("fact: El exponcial debe ser positivo.");
return poter(x,n);
}
//Calculo y presentacion del numero de digitos
function calcular(){
try{
var n=parseInt(document.getElementById("num").value);
var x=parseInt(document.getElementById("num1").value);
document.getElementById("res").value=pote(n);
document.getElementById("num").focus();
document.getElementById("num").select();
} catch(e){
alert("factorial: "+e.message);
}
}
//Valida el dato escrito en el input "num"
function vDato(){
var n=document.getElementById("num").value;
var x=document.getElementById("num1").value;
try{
if((isNaN(n)) && (isNaN(x))) throw new Error("Los datos deben numeros");
if(frac(n)!=0) throw new Error("El exponecial debe ser entero");
if(n<0) throw new Error("El exponecial debe ser positivo");
return true;
} catch(e){
alert(e.message);
document.getElementById("num").select();
document.getElementById("num").focus();
return false;
}
}
//valida la escritura de numeros naturales (enteros positivos)
function vNatural(event){
//tecla pulsada
var key=event.which;
//si la tecla pulsada esta entre 0 y 9
if (key>=48 && key<=57) return true;
//si la tecla es retroceso(8), tab(9), fin(35), inicio(36),
//cursor izquierdo (37), cursor derecho(39) o suprimir(46)
if (key==8 || key==9 || key==35 || key==36 || key==37 ||
key==39 || key==46) return true;
//si la tecla pulsada es Enter y el valor escrito es un numerp
if (key==13 && vDato()){
document.getElementById("btn1").onclick();
return true;
}
return false;
}
//Selecciona el input "num" al cargar la pagina
function iniciar(){
document.getElementById("num").focus();
document.getElementById("num").select();
}
</script>
</head>
<body onload="iniciar()">
<h3>potencia x^n</h3>
<label for="num">Introducir x :<br>
<input type="text" id="num" value="0" size="6"
onkeydown="return vNatural(event)"><br>
<label for="num">Introducir n :<br>
<input type="text" id="num1" value="0" size="6"
onkeydown="return vNatural(event)"><br>
<label for="res">Resultado:<br>
<input type="text" id="res" value="1" size="20" readonly><br>
<button type="button" id="btn1" onclick="calcular()"> Calcular
</button>
</body>
</html>