Skip to content

Instantly share code, notes, and snippets.

@haerang94
Last active July 3, 2020 05:43
Show Gist options
  • Save haerang94/f9c2e01fe0e808a584395bfe483f4726 to your computer and use it in GitHub Desktop.
Save haerang94/f9c2e01fe0e808a584395bfe483f4726 to your computer and use it in GitHub Desktop.
우아한테크캠프 3일차
function ClassifierAlpha(number=0) { //number 초기값은 0
this.number = number;
}
ClassifierAlpha.prototype.isFactor=function(potentialFactor) {
return this.number % potentialFactor === 0; //=== 타입까지 검사 , ==은 값의 비교만 해준다
}
ClassifierAlpha.prototype.factors=function() {
const factorSet = []; // 값의 할당 변경불가
for (let pod = 1; pod <= Math.sqrt(this.number); pod++ ) {
if (this.isFactor(pod)) {
factorSet.push(pod, this.number/pod); // 한 줄로 push해줌
// factorSet.push(this.number / pod);
}
}
return factorSet;
}
ClassifierAlpha.prototype.isPerfect=function() {
const currentFactor = this.factors();
return (this.sum(currentFactor) - this.number) === this.number;
}
ClassifierAlpha.prototype.isAbundant=function() {
const currentFactor = this.factors();
return (this.sum(currentFactor) - this.number) > this.number ;
}
ClassifierAlpha.prototype.isDeficient=function() {
const currentFactor = this.factors();
return (this.sum(currentFactor) - this.number) < this.number
}
ClassifierAlpha.prototype.sum=function(factors) {
return factors.reduce((acc,factor)=>acc+factor,0);
}
// result 메소드는 받게 되는 number가 perfect, deficient, abundant 중 어디에 해당하는 지 반환해준다
ClassifierAlpha.prototype.result=function(){
if(this.isPerfect())return 'perfect';
else if(this.isDeficient())return 'deficient';
else return 'abundant';
}
module.exports=ClassifierAlpha;
const ClassifierAlpha =require('./ ClassfierAlpha');
function PrimeAlpha(number=0) {
this.number = number;
}
PrimeAlpha.prototype.equalSet=function(aset, bset) {
if (aset.length!== bset.length) return false;
aset.map(a=>{
if (!bset.includes(a)) return false;
})
// for (let a of aset) if (!bset.includes(a)) return false;
return true;
}
PrimeAlpha.prototype.isPrime=function() {
const primeSet = [1, this.number];
return this.number > 1 && this.equalSet(this.factors(), primeSet);
}
PrimeAlpha.prototype.isFactor=function(potentialFactor) {
return this.number % potentialFactor === 0;
}
PrimeAlpha.prototype.factors=function() {
const factorSet = [];
for (let pod = 1; pod <= Math.sqrt(this.number); pod++ ) {
if (this.isFactor(pod)) {
factorSet.push(pod, this.number/pod); // 한 줄에 push 해줌
// factorSet.push(this.number / pod);
}
}
return factorSet;
}
const numbers=[2,3,4,5,6,7,8,9,10];
const result=numbers.reduce((acc,cur)=>{
return acc+`${cur} : ${new ClassifierAlpha(cur).result()}, ${new PrimeAlpha(cur).isPrime()?'prime':''}\n`;
},'');
console.log(result);
// var prime1 = new PrimeAlpha(10);
// var prime2 = new PrimeAlpha(7);
// console.log(prime1.isPrime());
// console.log(prime2.isPrime());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment