Created
August 7, 2019 22:10
-
-
Save parzibyte/8b227e65dcd6d963e07119927a692c57 to your computer and use it in GitHub Desktop.
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
const esNarcisista = numero => { | |
const numeroComoCadena = numero.toString(), | |
longitudDelNumero = numeroComoCadena.length; | |
let suma = 0; | |
// Recorrer la cadena, carácter por carácter | |
for (let indice = 0; indice < longitudDelNumero; indice++) { | |
// Tomar el carácter que esté en la cadena en la posición del índice | |
// y convertirlo a entero | |
/* | |
Recordemos que para elevar un número a una potencia hacemos: | |
elevado = numero ** potencia; | |
Lo mismo que hacer: | |
elevado = Math.pow(numero, potencia); | |
Más info: https://parzibyte.me/blog/2018/04/20/elevar-numero-potencia-javascript/ | |
*/ | |
let cifraComoEntero = parseInt(numeroComoCadena[indice]), | |
numeroElevado = cifraComoEntero ** longitudDelNumero; | |
suma = suma + numeroElevado; | |
} | |
if (suma === numero) return true; | |
else return false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment