tried to add very simple modern family structure. may include ordinary parents, single parents, biological & social parents, .....
class Human { constructor (firstName = '', lastName = '') { this.firstName = firstName; this.lastName = lastName; this.parents = []; // modern family. who takes parental care? } filterMyFamily(humans) { return humans.filter(human => human.lastName === this.lastName) } addParent(p){ if (!this.hasParent(p)) this.parents.push(p); return this; } hasChild(c){ return c.hasParent(this); } hasParent(p){ for (var i=0; i<this.parents.length; i++){ if (this.parents[i] === p) return true; } return false; } }
- class Human {
- constructor (firstName = '', lastName = '') {
- this.firstName = firstName;
- this.lastName = lastName;
- this.parents = []; // modern family. who takes parental care?
- }
- filterMyFamily(humans) {
- return humans.filter(human => human.lastName === this.lastName)
- }
- addParent(p){
- if (!this.hasParent(p)) this.parents.push(p);
- return this;
- }
- hasChild(c){
- return c.hasParent(this);
- }
- hasParent(p){
- for (var i=0; i<this.parents.length; i++){
- if (this.parents[i] === p) return true;
- }
- return false;
- }
- }
var expect = require("chai").expect let james = new Human("James","Black") let billy = new Human("Billy","White") let sam = new Human("Sam","Blue") let nick = new Human("Nick","Blue") let justSue = new Human("Sue") describe("constructor", function() { it("should initialize objects", function() { expect(james).to.be.an.instanceof(Human) //Test.expect(james instanceof Human, "james is a Human") }) it("fill default property values", function() { expect(justSue.lastName).to.be.a('string') }) }) describe("filterMyFamily", function(){ it("function works properly", function(){ let meeting = [james, billy, sam, nick] let filtered = nick.filterMyFamily(meeting) expect(filtered).to.be.a('array') expect(filtered).to.include(sam) expect(filtered).to.have.length.within(2,2) }); }); describe("parentsAndChilds", function(){ it("function works properly", function(){ expect(james.hasParent(billy)).to.be.equal(false); expect(james.addParent(billy)).to.be.an.instanceof(Human) expect(james.hasParent(billy)).to.be.equal(true); expect(billy.hasChild(james)).to.be.equal(true); }); });
- var expect = require("chai").expect
- let james = new Human("James","Black")
- let billy = new Human("Billy","White")
- let sam = new Human("Sam","Blue")
- let nick = new Human("Nick","Blue")
- let justSue = new Human("Sue")
- describe("constructor", function() {
- it("should initialize objects", function() {
- expect(james).to.be.an.instanceof(Human)
- //Test.expect(james instanceof Human, "james is a Human")
- })
- it("fill default property values", function() {
- expect(justSue.lastName).to.be.a('string')
- })
- })
- describe("filterMyFamily", function(){
- it("function works properly", function(){
- let meeting = [james, billy, sam, nick]
- let filtered = nick.filterMyFamily(meeting)
- expect(filtered).to.be.a('array')
- expect(filtered).to.include(sam)
- expect(filtered).to.have.length.within(2,2)
- });
- });
- describe("parentsAndChilds", function(){
- it("function works properly", function(){
- expect(james.hasParent(billy)).to.be.equal(false);
- expect(james.addParent(billy)).to.be.an.instanceof(Human)
- expect(james.hasParent(billy)).to.be.equal(true);
- expect(billy.hasChild(james)).to.be.equal(true);
- });
- });