Created
September 8, 2017 07:39
Star
You must be signed in to star a gist
life
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
var life = function (o) { | |
var totalSec = 0; | |
var timer; | |
var time = function () { | |
}; | |
var random = function (from, to) { | |
return Math.floor((Math.random() * to) + from); | |
}; | |
var baby = function (name, surname) { | |
return new life({ | |
name: name, | |
surname: surname, | |
sex: random(0, 1) ? "male" : "female" | |
}); | |
}; | |
this.name = o.name; | |
this.surname = o.surname; | |
this.birth = new Date(); // 1min = 1year | |
this.soulMate = null; | |
this.sex = o.sex || "male"; // first human is MALE! | |
this.children = []; // u need soulMate | |
this.parent = {}; // u are a child of God ;) lucky human | |
this.getFullName = function () { | |
return this.name + " " + this.surname; | |
}; | |
this.setParent = function (o) { | |
parent.father = o.father; | |
parent.mother = o.mother; | |
}; | |
this.getAge = function () { | |
var now = new Date(); | |
var diffMs = now - this.birth; | |
var diffSec = Math.floor(diffMs / 1000); | |
return Math.floor(diffSec / 60); | |
}; | |
this.live = function () { | |
timer = setInterval(function () { | |
totalSec++; | |
time(); | |
}, 1000); | |
}; | |
this.death = function () { | |
clearInterval(timer); | |
}; | |
this.setSoulMate = function (o) { | |
if (o instanceof life) { | |
if ((this.sex === "male" && o.sex === "female") || | |
(this.sex == "female" && o.sex === "male")) { | |
this.soulMate = o; | |
o.soulMate = this; | |
} else { | |
throw new Error("homosexuality is not yet active :/"); | |
} | |
} else { | |
throw new Error("u need human dude!"); | |
} | |
}; | |
this.makeLove = function (name) { | |
if (this.surname === this.soulMate.surname) { | |
this.children.push(baby(name, this.surname)); | |
} else { | |
throw new Error("u need wife/husband!"); | |
} | |
}; | |
this.marriage = function () { | |
if (this.sex === "female") { | |
this.surname = this.soulMate.surname; | |
} else if (this.sex === "male") { | |
this.soulMate.surname = this.surname; | |
} else { | |
throw new Error("wtf are u doin'?"); | |
} | |
this.children = this.soulMate.children; | |
}; | |
}; | |
module.exports = life; |
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
var Code = require('code'); | |
var Lab = require('lab'); | |
var lab = exports.lab = Lab.script(); | |
var describe = lab.describe; | |
var it = lab.it; | |
var expect = Code.expect; | |
var life = require('../life'); | |
describe('life', function () { | |
it('name-surname', function (done) { | |
var eray = new life({ | |
name: "eray", | |
surname: "arslan" | |
}); | |
expect(eray.getFullName()).to.equal("eray arslan"); | |
done(); | |
}); | |
it('soul-mate', function (done) { | |
var eray = new life({ | |
name: "eray", | |
surname: "arslan", | |
sex: "male" | |
}); | |
var adriana = new life({ | |
name: "adriana", | |
surname: "lima", | |
sex: "female" | |
}); | |
eray.setSoulMate(adriana); | |
expect(adriana.soulMate.getFullName()).to.equal("eray arslan"); | |
done(); | |
}); | |
it('marriage', function (done) { | |
var eray = new life({ | |
name: "eray", | |
surname: "arslan", | |
sex: "male" | |
}); | |
var adriana = new life({ | |
name: "adriana", | |
surname: "lima", | |
sex: "female" | |
}); | |
eray.setSoulMate(adriana); | |
adriana.marriage(); | |
expect(eray.soulMate.getFullName()).to.equal("adriana arslan"); | |
done(); | |
}); | |
it('sex', function (done) { | |
var eray = new life({ | |
name: "eray", | |
surname: "arslan", | |
sex: "male" | |
}); | |
var adriana = new life({ | |
name: "adriana", | |
surname: "lima", | |
sex: "female" | |
}); | |
eray.setSoulMate(adriana); | |
adriana.marriage(); | |
eray.makeLove("masal"); | |
expect(adriana.children[0].getFullName()).to.equal("masal arslan"); | |
done(); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment