Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

137
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda