-
-
Save IrhaAli/95f3634242d1955aa5d5b8a2b91aa4ec to your computer and use it in GitHub Desktop.
Vampires family tree
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
class Vampire { | |
constructor(name, yearConverted) { | |
this.name = name; | |
this.yearConverted = yearConverted; | |
this.offspring = []; | |
this.creator = null; | |
} | |
/** Simple tree methods **/ | |
// Adds the vampire as an offspring of this vampire | |
addOffspring(vampire) { | |
this.offspring.push(vampire); | |
vampire.creator = this; | |
} | |
// Returns the total number of vampires created by that vampire | |
get numberOfOffspring() { | |
return this.offspring.length; | |
} | |
// Returns the number of vampires away from the original vampire this vampire is | |
get numberOfVampiresFromOriginal() { | |
let numberOfPeople = 0; | |
let currentVampire = this; | |
// climb "up" the tree (using iteration), counting nodes, until no boss is found | |
while (currentVampire.creator) { | |
currentVampire = currentVampire.creator; | |
numberOfPeople++; | |
} | |
return numberOfPeople; | |
} | |
// Returns true if this vampire is more senior than the other vampire. (Who is closer to the original vampire) | |
isMoreSeniorThan(vampire) { | |
return (this.numberOfVampiresFromOriginal < vampire.numberOfVampiresFromOriginal) ? true : false; | |
} | |
/** Tree traversal methods **/ | |
// Returns the vampire object with that name, or null if no vampire exists with that name | |
vampireWithName(name) { | |
let wantedVamp = null; | |
if (this.name === name) { | |
wantedVamp = this; | |
} | |
for (const child of this.offspring) { | |
const childVampSearch = child.vampireWithName(name); | |
if (childVampSearch) { | |
return childVampSearch; | |
} | |
} | |
return wantedVamp; | |
} | |
// Returns the total number of vampires that exist | |
get totalDescendents() { | |
let totalKids = this.offspring.length; | |
for (const child of this.offspring) { | |
totalKids += child.totalDescendents; | |
} | |
return totalKids; | |
} | |
// Returns an array of all the vampires that were converted after 1980 | |
get allMillennialVampires() { | |
let millenialVamps = []; | |
if (this.yearConverted > 1980) { | |
millenialVamps.push(this); | |
} | |
for (const child of this.offspring) { | |
millenialVamps = millenialVamps.concat(child.allMillennialVampires); | |
} | |
return millenialVamps; | |
} | |
getAncestors() { | |
let ancestors = [this]; | |
if (this.creator) { | |
ancestors = ancestors.concat(this.creator.getAncestors()); | |
} | |
return ancestors; | |
} | |
/** Stretch **/ | |
// Returns the closest common ancestor of two vampires. | |
// The closest common anscestor should be the more senior vampire if a direct ancestor is used. | |
// For example: | |
// * when comparing Ansel and Sarah, Ansel is the closest common anscestor. | |
// * when comparing Ansel and Andrew, Ansel is the closest common anscestor. | |
closestCommonAncestor(vampire) { | |
// helper function to get all ancestors for this and vampire (recursive) | |
const myAncestors = this.getAncestors(); | |
const itsAncestors = vampire.getAncestors(); | |
// check for common ancestors | |
for (const myAncestor of myAncestors) { | |
for (const itsAncestor of itsAncestors) { | |
// return the closest common ancestor | |
if (myAncestor.name === itsAncestor.name) { | |
return myAncestor; | |
} | |
} | |
} | |
} | |
} | |
module.exports = Vampire; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment