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
41 changes: 21 additions & 20 deletions area.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,42 @@
const PIE = Math.PI;

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

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

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

module.exports.cylindrical = function(r, h){
checkParamCount(r, h);
checkParamType(r, h);
module.exports.cylindrical = function(r = -1, h = -1){
checkParameter(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(rest){
for (let i=0; i<rest.length; i++){
if(rest[i] === -1){
throw Error("모든 인자를 입력하세요.");
}
}
}

function checkParamCount(){
for (let i=0; i<arguments.length; i++){
if(typeof arguments[i] === "undefined"){
throw Error("모든 인자를 입력하세요.");
function checkParamType(rest){
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.

참고,
보통 rest라는 이름을 잘 안쓰긴해요.
parms 나 args 가 조금더 나아보이네요.

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

function checkParameter(...rest){
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.

잘하셨어요~!
중요한 로직이 아닌 것들은 이렇게 추상화시키면 분리하면 좋아요.

checkParamCount(rest);
checkParamType(rest);
}
2 changes: 1 addition & 1 deletion main.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const area = require("./area");

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

Expand Down