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.
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);