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

214
Visualizações
Return the number if it is an integer with one line

So I have function that I wanted to write as a one-liner code for the problem in codewars [problem link].

Here is the code that I have so far:

export class G964 {
    public static digPow = (n: number, p: number) => ((String(n).split('').reduce((sum: number, d: string) => sum + (+d) ** p++, 0) / n) | 0) || -1;
}

The issue here is that n | 0 return the rounded value, but what I'm looking for is something that can be used to check if the value is an integer (has no reminder) and if it is, return the value, however if it isn't, then return false or something else, that will allow me to return -1.

The main point is that I don't want to store or duplicate (String(n).split('').reduce((sum: number, d: string) => sum + (+d) ** p++, 0) / n) as a value.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Don't know if you can get around storing the value, but it's still pretty clean to do:

public static digPow = (n: number, p: number) =>  (n=(String(n).split('').reduce((sum: number , d: string) => sum + (+d) ** p++, 0) / n), n%1 ? -1:n);
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