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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
# polygon
레벨2
레벨2
41 changes: 41 additions & 0 deletions area.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
const PIE = Math.PI;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

PIE 가 먹고 싶어지..


module.exports.circle = function(r){
checkParamCount(r);
checkParamType(r);
return Math.pow(r, 2) * PIE;
}

module.exports.square = function(a, b){
checkParamCount(a, b);
checkParamType(a, b);
return a * b;
}

module.exports.trapezoid = function(a, b, h){
checkParamCount(a, b, h);
checkParamType(a, b, h);
return (a + b) * h / 2;
}

module.exports.cylindrical = function(r, h){
checkParamCount(r, h);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

이 두 개의 코드는 같은 역할로 묶을수도 있죠.
그 두개를 묶는 함수를 하나 만들고,
여기서는 그 함수를 호출하는 것도 방법일거 같네요.
(퍼사드패턴)

checkParamType(r, h);
return (this.circle(r) * 2) + (r * 2 * PIE * h);
}

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

function checkParamCount(){
for (let i=0; i<arguments.length; i++){
if(typeof arguments[i] === "undefined"){
throw Error("모든 인자를 입력하세요.");
}
}
}
11 changes: 11 additions & 0 deletions main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const area = require("./area");

console.log(area.circle(3));
console.log(area.square(4, 5));
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));