Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

132
Vistas
Javascript reduce method not returning expected output. What is going wrong here?

I am working on a codeWars challenge and am unsure why my reduce method isn't returning the expected outcome.

Here are the instructions:

A Narcissistic Number is a number of length n in which the sum of its digits to the power of n is equal to the original number. If this seems confusing, refer to the example below.

Ex: 153, where n = 3 (number of digits in 153) 1³ + 5³ + 3³ = 153

Write a method is_narcissistic(i) which returns whether or not i is a Narcissistic Number.

Here is my solution:

function narcissistic(value) {
  let digits = (value + '').split('').map(Number);
  let narc = digits.reduce((a, c) => a + (c ** digits.length))
  return narc === value;
}

console.log(narcissistic(371))

When I test it with 371, which is a narcissistic number, 341 is returned instead of 371

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

If you don't supply an initial value, the 1st item is used as the initial value, and it's not multiplied. Set the initial value to be 0.

function narcissistic(value) {
  let digits = (value + '').split('').map(Number);
  let narc = digits.reduce((a, c) => a + (c ** digits.length), 0)
  return narc === value;
}

console.log(narcissistic(371))

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda