Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 51 additions & 17 deletions area.js
Original file line number Diff line number Diff line change
@@ -1,42 +1,76 @@
const PIE = Math.PI;
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

변수명은 PI 가 좋을 듯?

let areaSum = 0;
let stackList = [];
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const를 써도 되겠죠?


module.exports.circle = function(r = -1){
function circle(r = -1){
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

그냥 circle보다는 areaCircle 같이 조금 더 명확한 이름이 좋습니다.
circle이라고 하면 코드의 내용까지 읽어야 어떤 함수인지를 알 수 있죠.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

다시 읽어보니 모듈의 이름이 area라서 circle도 괜찮을 것 같아요 😉

checkParameter(r);
stackList.push("circle");
return Math.pow(r, 2) * PIE;
}

module.exports.square = function(a = -1, b = -1){
function circleSum(r = -1){
checkParameter(r);

if(r > 0){
areaSum += Math.pow(r, 2) * PIE;
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이미 있는 함수를 재활용하는게 좋지 않을까요?

circleSum(r-1);
} else{
stackList.push("circle");
}

return areaSum;
}

function rect(a = -1, b = -1){
checkParameter(a, b);
stackList.push("rect");
return a * b;
}

module.exports.trapezoid = function(a = -1, b = -1, h = -1){
function trapezoid(a = -1, b = -1, h = -1){
checkParameter(a, b, h);
stackList.push("trapezoid");
return (a + b) * h / 2;
}

module.exports.cylindrical = function(r = -1, h = -1){
checkParameter(r, h);
return (this.circle(r) * 2) + (r * 2 * PIE * h);
}

function checkParamCount(rest){
for (let i=0; i<rest.length; i++){
if(rest[i] === -1){
function checkParamCount(param){
for (let i=0; i<param.length; i++){
if(param[i] === -1){
throw Error("모든 인자를 입력하세요.");
}
}
}

function checkParamType(rest){
for (let i=0; i<rest.length; i++) {
if(typeof rest[i] !== "number") {
function checkParamType(param){
for (let i=0; i<param.length; i++) {
if(typeof param[i] !== "number") {
throw Error("숫자를 입력하세요.");
}
}
}

function checkParameter(...rest){
checkParamCount(rest);
checkParamType(rest);
function checkParameter(...param){
checkParamCount(param);
checkParamType(param);
}

module.exports.printExecutionSequence = function(){
let result = stackList.join(', ');
return result;
}

module.exports.getArea = function (...param){
if (param[0] === 'circle'){
if(param.length >= 3){
return circleSum(param[2]);
}else{
return circle(param[1]);
}
} else if(param[0] === 'rect'){
return rect(param[1], param[2]);
} else if(param[0] === 'trapezoid'){
return trapezoid(param[1], param[2], param[3]);
}else{
throw Error("함수를 다시입력하세요.");
}
}
14 changes: 5 additions & 9 deletions main.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
const area = require("./area");

console.log(area.circle(3));
console.log(area.square(5, 6));
console.log(area.trapezoid(5, 3, 2));
console.log(area.cylindrical(2, 5));

// console.log(area.circle('3'));
// console.log(area.square('아', 5));
// console.log(area.trapezoid(5, 3, '3'));
// console.log(area.cylindrical(2));
console.log(area.getArea('circle', 10));
console.log(area.getArea('circle', 1, 4));
console.log(area.getArea('rect', 10, 15));
console.log(area.getArea('trapezoid', 10, 15, 12));
console.log(area.printExecutionSequence());