Skip to content

Instantly share code, notes, and snippets.

@parzibyte
Created August 7, 2019 22:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save parzibyte/8b227e65dcd6d963e07119927a692c57 to your computer and use it in GitHub Desktop.
Save parzibyte/8b227e65dcd6d963e07119927a692c57 to your computer and use it in GitHub Desktop.
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