Created
July 27, 2020 11:29
-
-
Save graynun/933c211b348b83026fd15e917a388ede to your computer and use it in GitHub Desktop.
Replace Conditional with Polymorphism
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
function plumages(birds) { | |
return new Map(birds.map(b => [b.name, plumage(b)])); | |
} | |
function speeds(birds) { | |
return new Map(birds.map(b => [b.name, airSpeedVelocity(b)])); | |
} | |
function plumage(bird) { | |
switch (bird.type) { | |
case '유럽 제비': | |
return "보통이다"; | |
case '아프리카 제비': | |
return (bird.numberOfCoconuts > 2) ? "지쳤다" : "보통이다"; | |
case '노르웨이 파랑 앵무': | |
return (bird.voltage > 100) ? "그을렸다" : "예쁘다"; | |
default: | |
return "알 수 없다"; | |
} | |
} | |
function airSpeedVelocity(bird) { | |
switch (bird.type) { | |
case '유럽 제비': | |
return 35; | |
case '아프리카 제비': | |
return 40 - 2 * bird.numberOfCoconuts; | |
case '노르웨이 파랑 앵무': | |
return (bird.isNailed) ? 0 : 10 + bird.voltage / 10; | |
default: | |
return null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment