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

208
Vistas
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 Respuestas
Responde la pregunta

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 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