Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

207
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!