Basic subclasses - Adam and Eve (8kyu): Codewars (TDD in JavaScript)

Jan 17, 2020 08:00 · 448 words · 3 minute read objects simple constructor wander first

Hi! It’s Friday. So let’s chill and do something easy today. If this is your first time here and you want to learn Frontend and JavaScript, start right now by subscribing, and don’t forget to turn on the subtitles. [Music playing] Let’s solve today kata titled “Basic Subclasses - Adam and Eve.” According to the creation myths of the Abrahamic religions, Adam and Eve were the first Humans to wander the Earth. You have to do God’s job. The creation method must return an array of length 2 containing objects (representing Adam and Eve).

00:40 - The first object in the array should be an instance of the class Man. The second should be an instance of the class Woman. Both objects have to be subclasses of Human. Your job is to implement the Human, Man and Woman classes. Okay, let’s try. So I have a class god that returns an array Human and have only one static “create” method. Okay, let’s examine test cases for the first one. Creates a variable using this “create” method, so it gets an instance of this class and it expects an area “humans” and the first element should be an instance of “Man.” Okay, that’s quite understandable. So I need to create a class “Human” as the first expected class. It is a class “Human” and it contains simply a constructor and nothing more. Then I need to create a class “Man,” it should inherit a class “Human,” so I need to extend a “Human” with a simple constructor, that returns nothing and that’s all. And then let’s extend this static method.

01:59 - I create a variable “Adam” and it should be an instance of “Man” and then return an array of this instance, “Adam.” Let’s try. Run sample tests. “Must call super constructor in derived class before,” so let’s extend this constructor using “super.” Let’s try. Okay, now that works. And for other test cases: it should be Woman. Let’s create Eve. So I need a class “Woman” that extends a class “Human” and do the same: constructor, inherits of all elements from the human and then returns a new instance. Let’s add “Eve” here, const “Eve” new from a woman. And add her into this array. Let’s try this solution. A keyword “super” is unexpected here. Let’s try. Now it works. I like it.

03:30 - [Music playing] My code is available on GitHub. Documentation is available in the Internet. Follow the links if you’re eager to get more. If you like this video give it “thumbs up”, share it with your friends, subscribe to the channel and watch other episodes. Thanks for watching and dive deeper. .