Skip to content

Instantly share code, notes, and snippets.

@myungwoo-Y
Last active July 3, 2020 05:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save myungwoo-Y/e2b95996e9cd2eaea875090ca93b0038 to your computer and use it in GitHub Desktop.
Save myungwoo-Y/e2b95996e9cd2eaea875090ca93b0038 to your computer and use it in GitHub Desktop.
/*
==와 ===의 차이
==는 비교시 각 타입에 따른 강제 형변환을 한 뒤 비교를 시도한다. 형변환 되는 내용은 각 타입마다 다를 수 있기 떄문에 예상치 못한 결과가 나올 수 있다.
===는 강제 형변환을 하지 않고 비교를 시도한다. 일반적으로 다른 언어에서 사용하던 ==와 비슷하다고 생각 할 수 있다.
타입에 대해 업격하기 때문에 더 정확한 결과를 얻을 수 있다.
*/
/*
var vs let,const
var
var는 호이스팅으로 인하여 실행 컨텍스트 생성시 선언이 최상위 스코프로 끌어올려진다. 때문에 var로 선언된 변수에 대해서는 같은 스코프라면
어디서든 접근 가능하다. 값은 기본으로 undefined로 할당된다.
let
함수 선언부분 이후에 변수에 접근 할 수 있다. 선언만 이루어지고 초기화가 이루어지지 않기 때문에 값을 표현식에서 할당 하기 이전에 접근 할 수 없다.
선언문이 등장한 이후에 변수에 접근 할 수 있다.
const
let과 비슷한 방식으로 작동한다. 마찬가지로 선언문이 등장한 이후에 변수에 접근 할 수 있다. let과 다른점은 한번 값을 할당한 후
더이상 값을 변경 할 수 없다는 점이다.
*/
const PrimeAlpha = (function(){
function PrimeAlpha(number) {
if(number === undefined){
this.number = 0;
}else{
this.number = number
}
}
PrimeAlpha.prototype.equalSet = function(aset, bset) {
if (aset.size!== bset.size) return false;
let flag = true;
// 기존 반복문을 수정한다.
aset.forEach((a) => {
if (!bset.has(a)){
flag = false;
return;
}
})
return flag;
}
PrimeAlpha.prototype.isPrime = function() {
const primeSet = new Set([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 = new Set();
for (let pod = 1; pod <= Math.sqrt(this.number); pod++ ) {
if (this.isFactor(pod)) {
factorSet.add(pod);
factorSet.add(this.number / pod);
}
}
return factorSet;
}
return PrimeAlpha;
})();
const ClassifierAlpha = (function(){
function ClassifierAlpha(number){
if(number === undefined){
this.number = 0;
}else{
this.number = number;
}
}
ClassifierAlpha.prototype.isFactor = function(potentialFactor) {
return this.number % potentialFactor == 0;
}
ClassifierAlpha.prototype.factors = function(){
const factorSet = new Set();
for (let pod = 1; pod <= Math.sqrt(this.number); pod++ ) {
if (this.isFactor(pod)) {
factorSet.add(pod);
factorSet.add(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) {
let total = 0;
factors.forEach( function(factor) {
total += factor;
});
return total;
}
return ClassifierAlpha;
})();
let data = [2, 3, 4, 5, 6, 7, 8, 9, 10];
const result = data.reduce((accumerate, number) => {
const classAlpha = new ClassifierAlpha (number);
const primeAlpha = new PrimeAlpha(number);
let result = '';
if(classAlpha.isAbundant()){
result += 'abundant, ';
}
if(classAlpha.isDeficient()){
result += 'deficient, ';
}
if(classAlpha.isPerfect()){
result += 'perfect, ';
}
if(primeAlpha.isPrime()){
result += 'prime';
}
return accumerate + `${number} : ` + result + '\n';
}, '')
console.log(result);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment