Created
June 26, 2019 03:01
-
-
Save pajaro5/c8be7949886a58e0001813ac6afae8eb to your computer and use it in GitHub Desktop.
Ejemplo de función recursiva que suma los pares entre 1 y 9
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>Suma Pares</title> | |
</head> | |
<body> | |
</body> | |
<script> | |
//1,2,3,4,5,6,7,8,9 | |
//Encontrar los pares y devolver la suma. | |
//Pares: 2,4,6,8 | |
//suma: 20 | |
//Funcion que determina si un numero es par | |
var esPar = function(numero) { | |
if (numero%2 === 0) { | |
return true; | |
}else{ | |
return false; | |
} | |
} | |
var sumaTotal = 0; //acumulador | |
var maximo = 9; | |
//repetidor, funcion recursiva. | |
var solver = function(contador){ | |
if (contador > maximo) { | |
return sumaTotal; | |
} else { | |
//logica de negocio | |
if (esPar(contador)) { | |
sumaTotal += contador; //2+4 | |
} | |
return solver(contador+1); | |
} | |
} | |
alert('suma de pares: ' + solver(1)); | |
</script> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment