Last active
January 19, 2021 08:25
-
-
Save skawnkk/920f901f2e1bd8eb62d05c8e26d279e3 to your computer and use it in GitHub Desktop.
Funtional Programming
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { | |
log | |
} = console; | |
const pipe = (...funcs) => v => { | |
return funcs.reduce((res, func) => { | |
return func(res); | |
}, v); | |
} | |
const factors = (num) => { | |
let isFactor = (pod) => num % pod == 0; | |
let arr = Array.from({ | |
length: parseInt(Math.sqrt(num)) | |
}, (_, i) => i + 1) | |
.filter(pod => isFactor(pod)) | |
.reduce((acc, pod) => acc.add(pod).add(num / pod), new Set); | |
return arr; | |
} | |
const sum = (factors) => { | |
let sumResult = Array.from(factors).reduce((acc, curr) => acc + curr, 0); | |
return sumResult; | |
} | |
let check = (num) => pipe(factors, sum)(num) - num; | |
//MISSION 1 | |
log('mission1-----------------------') | |
const isPerfect = (num) => check(num) == num; | |
const isAbundant = (num) => check > num; | |
const isDeficient = (num) => check < num; | |
const isPrime = (num) => check == 1; | |
log("case1:", isPerfect(10)); | |
log("case2:", isPrime(6)); | |
log("case3:", isDeficient(7)); | |
//MISSION2_TEST | |
log('mission2-----------------------') | |
const factorCheck = (num) => { | |
if (check(num) == num) 'Perfect'; | |
if (check(num) > num) return 'Abundant'; | |
if (check(num) > 1 && check(num) < num) return 'Deficient'; | |
if (check(num) == 1) return 'Deficient, Prime'; | |
} | |
const hundred = (maxNum) => { | |
return Array.from({ | |
length: maxNum - 1 | |
}, (_, i) => 2 + i); | |
} | |
const result = (resultArray) => { | |
return resultArray.reduce((acc, curr) => { | |
acc += `${curr}:${factorCheck(curr)}\n`; | |
return acc; | |
}, ''); | |
} | |
log(pipe(hundred, result)(100)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment