Created
July 2, 2019 21:51
-
-
Save pajaro5/fe5f0c492f6f50fe761eb4d46ce21f0c to your computer and use it in GitHub Desktop.
Resuelve el problema 1 de project euler, el usuario ingresa los datos en un formulario HTML
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="es"> | |
<head> | |
<meta charset="UTF-8"> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
<meta http-equiv="X-UA-Compatible" content="ie=edge"> | |
<title>Fibonacci Solver</title> | |
</head> | |
<body> | |
<h1>Fibonacci Solver</h1> | |
<p>Bienvenido amante de las matemáticas, esta página es un generador de la serie Fibonacci</p> | |
<p>Por favor ingresa la información solicitada y luego de clic en el botón generar</p> | |
<form action=""> | |
<label for="fibo1">Ingrese el primer elemento de la serie: </label> | |
<input type="text" name="fibo1" id="fibo1"> | |
<br> | |
<label for="fibo2">Ingrese el segundo elemento de la serie: </label> | |
<input type="text" name="fibo2" id="fibo2"> | |
<br> | |
<label for="maximo">Ingrese el número máximo de la serie: </label> | |
<input type="text" name="maximo" id="maximo"> | |
<br> | |
<br> | |
<input type="button" value="Calcular serie" id="botonSolver"> | |
<hr> | |
</form> | |
</body> | |
<script> | |
var primerElemento = 0; | |
var segundoElemento = 0; | |
var maximoValorSerie = 0; | |
var resultado = { | |
sumaPares : 0, | |
listaPares : [], | |
listaTodos : [] | |
} | |
var esPar = function(numero){ | |
if (numero%2 === 0) { | |
return true; | |
} else { | |
return false; | |
} | |
} | |
var generadorFibonacci = function(anterior,ultimo){ | |
if (anterior + ultimo > maximoValorSerie) { | |
return resultado; | |
} else { | |
nuevo = anterior + ultimo; | |
if(esPar(nuevo)){ | |
resultado.sumaPares += nuevo; | |
resultado.listaPares.push(nuevo); //inserta en el array de pares | |
} | |
resultado.listaTodos.push(nuevo); //inserta en el array de todos los elementos | |
return generadorFibonacci(ultimo,nuevo); | |
} | |
} | |
document.getElementById("botonSolver").addEventListener( | |
"click", | |
function(){ | |
primerElemento = parseInt(document.getElementById("fibo1").value); | |
segundoElemento = parseInt(document.getElementById("fibo2").value); | |
maximoValorSerie = parseInt(document.getElementById("maximo").value); | |
if(esPar(primerElemento)){ | |
resultado.listaPares.push(primerElemento); | |
} | |
if(esPar(segundoElemento)){ | |
resultado.listaPares.push(segundoElemento); | |
} | |
alert(generadorFibonacci(primerElemento,segundoElemento).sumaPares); | |
} | |
); | |
//Prueba con numeros de la serie menores a 100 | |
//serie: 1,2,3,5,8,13,21,34,55,89 | |
//suma pares: 44 | |
</script> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment