Last active
April 25, 2023 12:51
-
-
Save crongro/355438f8f47dd646825e19e4cedee701 to your computer and use it in GitHub Desktop.
test code source
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
export const printExecutionSequence = function() { | |
console.log(executionSequence.printAsStr()); | |
return executionSequence.printAsStr(); | |
} | |
export const executionSequence = (function() { | |
const sequence = []; | |
return { | |
update(str) {sequence.push(str);}, | |
printAsStr() { | |
const len = sequence.length; | |
let resultStr = `κ³μ°μνμμ : ${sequence[0]}`; | |
if (len === 1) {return resultStr} | |
for(let i = 1; i < len; i++) { | |
resultStr += `, ${sequence[i]}`; | |
} | |
return resultStr | |
} | |
} | |
})(); | |
/* | |
λμ΄λ₯Ό κ³μ°νκ³ , κ²°κ³Όλ₯Ό μΆλ ₯νλ λ©μλ(μ€νμμλ₯Ό λ³λ Objectμ μ μ₯) | |
*/ | |
export const computeArea = { | |
circle(radius) { | |
return Math.PI * Math.pow(radius, 2); | |
}, | |
rect(width, height) { | |
return width * height; | |
}, | |
trapezoid(top, bottom, height) { | |
return 1/2 * (top + bottom) * height; | |
}, | |
cylinder(radius, height) { | |
const circleSize = Math.PI * Math.pow(radius, 2); | |
const circumference = 2 * Math.PI * radius | |
return (circumference * height) + (2 * circleSize); | |
} | |
}; | |
export const printArea = { | |
//Return value through console.log | |
//Log function call history on executionSequence object | |
circle(circleRadius, circleArea) { | |
executionSequence.update('circle'); | |
console.log(`μ λ ₯νμ λ°μ§λ¦ ${circleRadius}μ μ λμ΄λ ${circleArea}μ λλ€.`); | |
}, | |
rect(rectWidth, rectHeight, rectArea) { | |
executionSequence.update('rect'); | |
console.log(`μ λ ₯νμ λλΉ ${rectWidth}, λμ΄ ${rectHeight}μ μ¬κ°ν λμ΄λ ${rectArea}μ λλ€.`); | |
}, | |
trapezoid(trapeTop, trapeBottom, trapeHeight, trapeArea) { | |
executionSequence.update('trapezoid'); | |
console.log(`μ λ ₯νμ μλ³ ${trapeTop}, μλ«λ³ ${trapeBottom}, λμ΄ ${trapeHeight}μ μ¬λ€λ¦¬κΌ΄ λμ΄λ ${trapeArea}μ λλ€.`); | |
}, | |
cylinder(cylinRadius, cylinHeight, cylinArea) { | |
executionSequence.update('cylinder'); | |
console.log(`μ λ ₯νμ λλΉ ${cylinRadius}, λμ΄ ${cylinHeight}μ μκΈ°λ₯ λμ΄λ ${cylinArea}μ λλ€.`); | |
} | |
}; | |
/* | |
μ λ ₯ μ€λ₯ μ κ² λ©μλ | |
*/ | |
export const inputErr = { //inputErr.argsNumber.one(arr) || inputErr.argsType(arr) | |
hasWrongNumOfElems(arr, rightNumOfArgs) {// Check # of arguments if it's less or more | |
if(!arr[0]) { | |
console.log('μ무 μ 보λ μ λ ₯νμ§ μμμ΅λλ€'); | |
return true; | |
} | |
if(arr.length < rightNumOfArgs) { | |
console.log('μ 보λ₯Ό λ μ λ ₯νμ μΌ ν©λλ€.'); | |
return true; | |
} else if(arr.length > rightNumOfArgs) { | |
console.log('λ무 λ§μ μ 보λ₯Ό μ λ ₯νμ ¨μ΅λλ€'); | |
return true; | |
} | |
}, | |
argsType(arr) { // Check element of arguments if any NAN or negative/zero Integer | |
let argsTypeError = false; | |
function hasWrongInt(arr) { | |
function isElemNegativeOrZero(arr) {return arr.some((el) => el <= 0)} | |
if (isElemNegativeOrZero(arr)) { | |
console.log('0λ³΄λ€ μκ±°λ κ°μ κ°μ΄ μμ΅λλ€!'); | |
argsTypeError = true; | |
} | |
} | |
function hasAnyNaN(arr) { | |
function nonNumberElem(arr) {return arr.some((el) => (typeof el != "number")? true : false)} | |
if (nonNumberElem(arr)) { | |
console.log('μ«μκ° μλ μ λ ₯κ°μ΄ μμ΅λλ€!'); | |
argsTypeError = true; | |
} | |
} | |
hasWrongInt(arr); | |
hasAnyNaN(arr); | |
return argsTypeError; | |
} | |
}; | |
/* | |
λμ΄ μ²λ¦¬ λ©μλ | |
*/ | |
export const area = { | |
circle(circleRadius) { | |
const circleArea = computeArea.circle(circleRadius); | |
const argList = Array.from(arguments); | |
if(inputErr.hasWrongNumOfElems(argList, 1) || inputErr.argsType(argList)) {return false;} | |
printArea.circle(circleRadius, circleArea); | |
}, | |
growingCircle(startRadius, targetRadius) { | |
const argList = Array.from(arguments); | |
if(inputErr.hasWrongNumOfElems(argList, 2) || inputErr.argsType(argList)) {return false;} | |
if(startRadius > targetRadius){ | |
console.log('μμ λ°μ§λ¦ λμ΄κ° μ΅μ’ λ°μ§λ¦λ³΄λ€ μμ΅λλ€!'); | |
return false; | |
} | |
for (let radius = startRadius; radius <= targetRadius; radius++) { | |
const circleArea = computeArea.circle(radius); | |
printArea.circle(radius, circleArea); | |
} | |
}, | |
rect(rectWidth, rectHeight) { | |
const rectArea = computeArea.rect(rectWidth, rectHeight); | |
const argList = Array.from(arguments); | |
if(inputErr.hasWrongNumOfElems(argList, 2) || inputErr.argsType(argList)) {return false;} | |
printArea.rect(rectWidth, rectHeight, rectArea); | |
}, | |
trape(trapeTop, trapeBottom, trapeHeight) { | |
const trapeArea = computeArea.trapezoid(trapeTop, trapeBottom, trapeHeight); | |
const argList = Array.from(arguments); | |
if(inputErr.hasWrongNumOfElems(argList, 3) || inputErr.argsType(argList)) {return false;} | |
printArea.trapezoid(trapeTop, trapeBottom, trapeHeight, trapeArea); | |
}, | |
cylin(cylinRadius, cylinHeight) { | |
const cylinArea = computeArea.cylinder(cylinRadius, cylinHeight); | |
const argList = Array.from(arguments); | |
if(inputErr.hasWrongNumOfElems(argList, 2) || inputErr.argsType(argList)) {return false;} | |
printArea.cylinder(cylinRadius, cylinHeight, cylinArea); | |
} | |
}; | |
/* | |
λμ΄ μ²λ¦¬ λ©μλλ₯Ό μ¬μ©μκ° νΈμΆνκ² λλ λ©μλ | |
*/ | |
export const getArea = function (...arg) { | |
let calcType = arg[0]; | |
const argList = arg.splice(1); | |
if(calcType === 'circle' && argList.length >= 2) {calcType = 'growingCircle';} | |
switch (calcType) { | |
case 'circle': | |
area.circle(...argList); | |
break; | |
case 'growingCircle': | |
area.growingCircle(...argList); | |
break; | |
case 'rect': | |
area.rect(...argList); | |
break; | |
case 'trapezoid': | |
area.trape(...argList); | |
break; | |
case 'cylinder': | |
area.cylin(...argList); | |
break; | |
default: | |
break; | |
} | |
} | |
function greeting() { | |
console.log( | |
`λ€κ°ν λμ΄ κ³μ°κΈ°μ λλ€. μλ μμλ₯Ό λ³΄κ³ λͺ λ Ήμ΄λ₯Ό μ λ ₯ν΄ μ£ΌμΈμ | |
- μ λμ΄: getArea('circle', λ°μ§λ¦ μ«μ) [μ: getArea('circle', 2)] | |
- μ μ 컀μ§λ μ λμ΄: getArea('circle', μμ λ°μ§λ¦ μ«μ, μ΅λ λ°μ§λ¦ μ«μ) [μ: getArea('circle', 2, 5)] | |
- μ¬κ°ν: getArea('rect', λλΉ, λμ΄) [μ: getArea('rect', 2, 3)] | |
- μ¬λ€λ¦¬κΌ΄: getArea('trapezoid', μλ³ κΈΈμ΄, μλ«λ³ κΈΈμ΄, λμ΄) [μ: area('trapezoid', 2,3,5)] | |
- μκΈ°λ₯: getArea('cylinder', λ°μ§λ¦, λμ΄) [μ: getArea('cylinder', 2,5)] | |
` | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment