A Pen by Yerlin Matu on CodePen.
Created
December 25, 2017 22:42
-
-
Save YerlinMatu/13336aa84ce9c0d0b6d469d0a19c87db to your computer and use it in GitHub Desktop.
FP - Recursividad e Inmersión
This file contains 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
// PROGRAMACIÓN FUNCIONAL. | |
// Función para convertir un número flotante en entero sin ( parseInt, Number, +). | |
// tip para convertir un número flotante en entero. ( | 0 Operador binario ). | |
function domWrite(x) { | |
return document.write(x + '<br>') | |
} | |
function converInt(value) { | |
return 0 | value; | |
} | |
// test. | |
domWrite(converInt(2.443)); // 2 | |
// Función para detectar si un número es positivo o negativo sin (if o else). | |
// tip Operador ternario con tabulación para condiciones. | |
function PosONeg(n) { | |
return n < 0 ? | |
'Número negativo' : 'Número positivo' | |
} | |
// test. | |
domWrite(PosONeg(-4)); | |
// Función para detectar si un número es par(true) o impar(false) sin( % ); | |
// Uso de la recursividad. | |
function par_O_impar(n) { | |
return n === 0 ? | |
true : !par_O_impar(n-1); | |
} | |
// test. | |
domWrite(par_O_impar(5)) | |
// Función que suma los digitos de un entero y retorna su respuesta. | |
// Recursividad simple. | |
function digits(n) { | |
return n < 10 ? n : digits( Math.floor(n / 10) + n % 10 ); | |
} | |
// test | |
domWrite(digits(423)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment