for…of statement depth-first traversal of a tree javascript
up vote
1
down vote
favorite
I'm trying to wrap my head around ES6 class declaration, instances of a class, the for...of statement, and recursive depth-first traversal of a tree.
Here is my tree:
And here is the construction of the tree:
// let index = Symbol('index');
class Royal {
constructor(name, yearBorn) {
this.name = name;
this.yearBorn = yearBorn;
this.royalParent = null;
// this.royalChildren = ;
this.royalChildren = new Array();
// this[index] = 0;
}
// [Symbol.iterator]() {
// let index = 0;
// return {
// next: () => ({
// value: this.offspring[index++],
// done: index > this.offspring.length
// })
// };
// }
// Adds royal child as a child of this royal parent
addRoyalChild(royalChild) {
// this[this[index]] = royalChild;
// this[index]++;
royalChild.royalParent = this;
this.royalChildren.push(royalChild);
// console.log('this[index]: ', this[index]);
}
// Returns the total number of direct offspring of this royal parent
get numberOfRoyalChildren() {
return `Should be first royal child: ${this.royalChildren}`;
// return this.royalChildren.length;
}
// Returns the number of generations away from the original royal (George V) that this royal is
get numberOfStepsFromOriginal() {
let numberOfSteps = 0;
let currentRoyalChild = this;
while (currentRoyalChild.royalParent) {
currentRoyalChild = currentRoyalChild.royalParent;
numberOfSteps++;
}
return numberOfSteps;
}
// Returns true if this royal is more senior than the other royal. i.e. Who is closer to the original royal?
isMoreSeniorThan(royalChild) {
if ((this.numberOfStepsFromOriginal - royalChild.numberOfStepsFromOriginal) < 0) {
return true;
}
return false;
}
allRoyals() {
let royals = ;
if (Array.isArray(this.royalChildren)) {
royals.push(this);
for (const royalChild of this.royalChildren) {
const royalChildrenArray = royalChild.allRoyals();
royals = royals.concat(royalChildrenArray);
}
} else {
console.log('Oy!');
}
return royals;
}
/** Tree traversal methods **/
// Returns the royal object with that name, or null if no royal exists with that name
royalWithName(name1, name2) {
let royals = new Array();
if (Array.isArray(this.royalChildren)) {
if ((this.name === name1) || (this.name === name2)) {
royals.push(this);
}
for (let royal of this.royalChildren) {
let royalsWithMatchingName = new Array();
royalsWithMatchingName = royal.royalWithName(name1, name2);
royals = royals.concat(royalsWithMatchingName);
}
} else {
return null;
}
return royals;
}
// Returns the number of descendents of a royal
get totalDescendents() {
return this.allRoyals().slice(1).length;
}
}
const georgeV = new Royal('George V', 1865);
const edwardVIII = new Royal('Edward VIII', 1894);
const georgeVI = new Royal('George VI', 1895);
const elizabethII = new Royal('Elizabeth II', 1926);
const margaret = new Royal('Princess Margaret', 1930);
const charles = new Royal('Prince Charles', 1948);
const anne = new Royal('Princess Anne', 1950);
const andrew = new Royal('Prince Andrew', 1960);
const edward = new Royal('Prince Edward', 1964);
const william = new Royal('Prince William', 1982);
const harry = new Royal('Prince Harry', 1984);
const beatrice = new Royal('Princess Beatrice', 1988);
const eugenie = new Royal('Princess Eugenie', 1990);
const louise = new Royal('Lady Louise', 2003);
const severn = new Royal('Viscount Severn', 2007);
georgeV.addRoyalChild(edwardVIII);
georgeV.addRoyalChild(georgeVI);
georgeVI.addRoyalChild(elizabethII);
georgeVI.addRoyalChild(margaret);
elizabethII.addRoyalChild(charles);
elizabethII.addRoyalChild(anne);
elizabethII.addRoyalChild(andrew);
elizabethII.addRoyalChild(edward);
charles.addRoyalChild(william);
charles.addRoyalChild(harry);
andrew.addRoyalChild(beatrice);
andrew.addRoyalChild(eugenie);
edward.addRoyalChild(louise);
edward.addRoyalChild(severn);
console.log('georgeV instanceof Royal:', georgeV instanceof Royal);
console.log('elizabethII.totalDescendents: ', elizabethII.totalDescendents);
console.log('typeof Royal: ', typeof Royal);
console.log('Royal.prototype: ', Royal.prototype);
// console.log('georgeV.constructor: ', georgeV.constructor);
console.log('royal with name Beatrice or Harry: ', georgeV.royalWithName('Princess Beatrice', 'Prince Harry'));
My burning question is this: How do I extract the objects out of the array that is returned from this method:
royalWithName(name1, name2) {
let royals = new Array();
if (Array.isArray(this.royalChildren)) {
if ((this.name === name1) || (this.name === name2)) {
royals.push(this);
}
for (let royal of this.royalChildren) {
let royalsWithMatchingName = new Array();
royalsWithMatchingName = royal.royalWithName(name1, name2);
royals = royals.concat(royalsWithMatchingName);
}
} else {
return null;
}
return royals;
}
This is node:
royal with name Beatrice or Harry: [ Royal {
name: 'Prince Harry',
yearBorn: 1984,
royalParent:
Royal {
name: 'Prince Charles',
yearBorn: 1948,
royalParent: [Object],
royalChildren: [Array] },
royalChildren: },
Royal {
name: 'Princess Beatrice',
yearBorn: 1988,
royalParent:
Royal {
name: 'Prince Andrew',
yearBorn: 1960,
royalParent: [Object],
royalChildren: [Array] },
royalChildren: } ]
All I would like to do is extract the objects from the array. (array.length = 0 (-1 + 1?))
Symbols, prototypes, iterables... What I would love is a working example of the method above. Links would be appreciated too.
javascript class oop depth-first-search for-of-loop
add a comment |
up vote
1
down vote
favorite
I'm trying to wrap my head around ES6 class declaration, instances of a class, the for...of statement, and recursive depth-first traversal of a tree.
Here is my tree:
And here is the construction of the tree:
// let index = Symbol('index');
class Royal {
constructor(name, yearBorn) {
this.name = name;
this.yearBorn = yearBorn;
this.royalParent = null;
// this.royalChildren = ;
this.royalChildren = new Array();
// this[index] = 0;
}
// [Symbol.iterator]() {
// let index = 0;
// return {
// next: () => ({
// value: this.offspring[index++],
// done: index > this.offspring.length
// })
// };
// }
// Adds royal child as a child of this royal parent
addRoyalChild(royalChild) {
// this[this[index]] = royalChild;
// this[index]++;
royalChild.royalParent = this;
this.royalChildren.push(royalChild);
// console.log('this[index]: ', this[index]);
}
// Returns the total number of direct offspring of this royal parent
get numberOfRoyalChildren() {
return `Should be first royal child: ${this.royalChildren}`;
// return this.royalChildren.length;
}
// Returns the number of generations away from the original royal (George V) that this royal is
get numberOfStepsFromOriginal() {
let numberOfSteps = 0;
let currentRoyalChild = this;
while (currentRoyalChild.royalParent) {
currentRoyalChild = currentRoyalChild.royalParent;
numberOfSteps++;
}
return numberOfSteps;
}
// Returns true if this royal is more senior than the other royal. i.e. Who is closer to the original royal?
isMoreSeniorThan(royalChild) {
if ((this.numberOfStepsFromOriginal - royalChild.numberOfStepsFromOriginal) < 0) {
return true;
}
return false;
}
allRoyals() {
let royals = ;
if (Array.isArray(this.royalChildren)) {
royals.push(this);
for (const royalChild of this.royalChildren) {
const royalChildrenArray = royalChild.allRoyals();
royals = royals.concat(royalChildrenArray);
}
} else {
console.log('Oy!');
}
return royals;
}
/** Tree traversal methods **/
// Returns the royal object with that name, or null if no royal exists with that name
royalWithName(name1, name2) {
let royals = new Array();
if (Array.isArray(this.royalChildren)) {
if ((this.name === name1) || (this.name === name2)) {
royals.push(this);
}
for (let royal of this.royalChildren) {
let royalsWithMatchingName = new Array();
royalsWithMatchingName = royal.royalWithName(name1, name2);
royals = royals.concat(royalsWithMatchingName);
}
} else {
return null;
}
return royals;
}
// Returns the number of descendents of a royal
get totalDescendents() {
return this.allRoyals().slice(1).length;
}
}
const georgeV = new Royal('George V', 1865);
const edwardVIII = new Royal('Edward VIII', 1894);
const georgeVI = new Royal('George VI', 1895);
const elizabethII = new Royal('Elizabeth II', 1926);
const margaret = new Royal('Princess Margaret', 1930);
const charles = new Royal('Prince Charles', 1948);
const anne = new Royal('Princess Anne', 1950);
const andrew = new Royal('Prince Andrew', 1960);
const edward = new Royal('Prince Edward', 1964);
const william = new Royal('Prince William', 1982);
const harry = new Royal('Prince Harry', 1984);
const beatrice = new Royal('Princess Beatrice', 1988);
const eugenie = new Royal('Princess Eugenie', 1990);
const louise = new Royal('Lady Louise', 2003);
const severn = new Royal('Viscount Severn', 2007);
georgeV.addRoyalChild(edwardVIII);
georgeV.addRoyalChild(georgeVI);
georgeVI.addRoyalChild(elizabethII);
georgeVI.addRoyalChild(margaret);
elizabethII.addRoyalChild(charles);
elizabethII.addRoyalChild(anne);
elizabethII.addRoyalChild(andrew);
elizabethII.addRoyalChild(edward);
charles.addRoyalChild(william);
charles.addRoyalChild(harry);
andrew.addRoyalChild(beatrice);
andrew.addRoyalChild(eugenie);
edward.addRoyalChild(louise);
edward.addRoyalChild(severn);
console.log('georgeV instanceof Royal:', georgeV instanceof Royal);
console.log('elizabethII.totalDescendents: ', elizabethII.totalDescendents);
console.log('typeof Royal: ', typeof Royal);
console.log('Royal.prototype: ', Royal.prototype);
// console.log('georgeV.constructor: ', georgeV.constructor);
console.log('royal with name Beatrice or Harry: ', georgeV.royalWithName('Princess Beatrice', 'Prince Harry'));
My burning question is this: How do I extract the objects out of the array that is returned from this method:
royalWithName(name1, name2) {
let royals = new Array();
if (Array.isArray(this.royalChildren)) {
if ((this.name === name1) || (this.name === name2)) {
royals.push(this);
}
for (let royal of this.royalChildren) {
let royalsWithMatchingName = new Array();
royalsWithMatchingName = royal.royalWithName(name1, name2);
royals = royals.concat(royalsWithMatchingName);
}
} else {
return null;
}
return royals;
}
This is node:
royal with name Beatrice or Harry: [ Royal {
name: 'Prince Harry',
yearBorn: 1984,
royalParent:
Royal {
name: 'Prince Charles',
yearBorn: 1948,
royalParent: [Object],
royalChildren: [Array] },
royalChildren: },
Royal {
name: 'Princess Beatrice',
yearBorn: 1988,
royalParent:
Royal {
name: 'Prince Andrew',
yearBorn: 1960,
royalParent: [Object],
royalChildren: [Array] },
royalChildren: } ]
All I would like to do is extract the objects from the array. (array.length = 0 (-1 + 1?))
Symbols, prototypes, iterables... What I would love is a working example of the method above. Links would be appreciated too.
javascript class oop depth-first-search for-of-loop
1
I'm not entirely clear what you mean by "All I would like to do is extract the objects from the array." What is your expected output?
– ggorlen
Nov 7 at 18:30
I can get the array. With name1 and name2 there are two objects inside the array. Console.log works. I want to return royal object[0] or maybe [1].
– cocomatt
Nov 7 at 18:36
1
array[0]
orarray[1]
isn't working for that?
– ggorlen
Nov 7 at 18:42
console.log(array[0]) works; return array[0] doesn't
– cocomatt
Nov 7 at 18:43
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I'm trying to wrap my head around ES6 class declaration, instances of a class, the for...of statement, and recursive depth-first traversal of a tree.
Here is my tree:
And here is the construction of the tree:
// let index = Symbol('index');
class Royal {
constructor(name, yearBorn) {
this.name = name;
this.yearBorn = yearBorn;
this.royalParent = null;
// this.royalChildren = ;
this.royalChildren = new Array();
// this[index] = 0;
}
// [Symbol.iterator]() {
// let index = 0;
// return {
// next: () => ({
// value: this.offspring[index++],
// done: index > this.offspring.length
// })
// };
// }
// Adds royal child as a child of this royal parent
addRoyalChild(royalChild) {
// this[this[index]] = royalChild;
// this[index]++;
royalChild.royalParent = this;
this.royalChildren.push(royalChild);
// console.log('this[index]: ', this[index]);
}
// Returns the total number of direct offspring of this royal parent
get numberOfRoyalChildren() {
return `Should be first royal child: ${this.royalChildren}`;
// return this.royalChildren.length;
}
// Returns the number of generations away from the original royal (George V) that this royal is
get numberOfStepsFromOriginal() {
let numberOfSteps = 0;
let currentRoyalChild = this;
while (currentRoyalChild.royalParent) {
currentRoyalChild = currentRoyalChild.royalParent;
numberOfSteps++;
}
return numberOfSteps;
}
// Returns true if this royal is more senior than the other royal. i.e. Who is closer to the original royal?
isMoreSeniorThan(royalChild) {
if ((this.numberOfStepsFromOriginal - royalChild.numberOfStepsFromOriginal) < 0) {
return true;
}
return false;
}
allRoyals() {
let royals = ;
if (Array.isArray(this.royalChildren)) {
royals.push(this);
for (const royalChild of this.royalChildren) {
const royalChildrenArray = royalChild.allRoyals();
royals = royals.concat(royalChildrenArray);
}
} else {
console.log('Oy!');
}
return royals;
}
/** Tree traversal methods **/
// Returns the royal object with that name, or null if no royal exists with that name
royalWithName(name1, name2) {
let royals = new Array();
if (Array.isArray(this.royalChildren)) {
if ((this.name === name1) || (this.name === name2)) {
royals.push(this);
}
for (let royal of this.royalChildren) {
let royalsWithMatchingName = new Array();
royalsWithMatchingName = royal.royalWithName(name1, name2);
royals = royals.concat(royalsWithMatchingName);
}
} else {
return null;
}
return royals;
}
// Returns the number of descendents of a royal
get totalDescendents() {
return this.allRoyals().slice(1).length;
}
}
const georgeV = new Royal('George V', 1865);
const edwardVIII = new Royal('Edward VIII', 1894);
const georgeVI = new Royal('George VI', 1895);
const elizabethII = new Royal('Elizabeth II', 1926);
const margaret = new Royal('Princess Margaret', 1930);
const charles = new Royal('Prince Charles', 1948);
const anne = new Royal('Princess Anne', 1950);
const andrew = new Royal('Prince Andrew', 1960);
const edward = new Royal('Prince Edward', 1964);
const william = new Royal('Prince William', 1982);
const harry = new Royal('Prince Harry', 1984);
const beatrice = new Royal('Princess Beatrice', 1988);
const eugenie = new Royal('Princess Eugenie', 1990);
const louise = new Royal('Lady Louise', 2003);
const severn = new Royal('Viscount Severn', 2007);
georgeV.addRoyalChild(edwardVIII);
georgeV.addRoyalChild(georgeVI);
georgeVI.addRoyalChild(elizabethII);
georgeVI.addRoyalChild(margaret);
elizabethII.addRoyalChild(charles);
elizabethII.addRoyalChild(anne);
elizabethII.addRoyalChild(andrew);
elizabethII.addRoyalChild(edward);
charles.addRoyalChild(william);
charles.addRoyalChild(harry);
andrew.addRoyalChild(beatrice);
andrew.addRoyalChild(eugenie);
edward.addRoyalChild(louise);
edward.addRoyalChild(severn);
console.log('georgeV instanceof Royal:', georgeV instanceof Royal);
console.log('elizabethII.totalDescendents: ', elizabethII.totalDescendents);
console.log('typeof Royal: ', typeof Royal);
console.log('Royal.prototype: ', Royal.prototype);
// console.log('georgeV.constructor: ', georgeV.constructor);
console.log('royal with name Beatrice or Harry: ', georgeV.royalWithName('Princess Beatrice', 'Prince Harry'));
My burning question is this: How do I extract the objects out of the array that is returned from this method:
royalWithName(name1, name2) {
let royals = new Array();
if (Array.isArray(this.royalChildren)) {
if ((this.name === name1) || (this.name === name2)) {
royals.push(this);
}
for (let royal of this.royalChildren) {
let royalsWithMatchingName = new Array();
royalsWithMatchingName = royal.royalWithName(name1, name2);
royals = royals.concat(royalsWithMatchingName);
}
} else {
return null;
}
return royals;
}
This is node:
royal with name Beatrice or Harry: [ Royal {
name: 'Prince Harry',
yearBorn: 1984,
royalParent:
Royal {
name: 'Prince Charles',
yearBorn: 1948,
royalParent: [Object],
royalChildren: [Array] },
royalChildren: },
Royal {
name: 'Princess Beatrice',
yearBorn: 1988,
royalParent:
Royal {
name: 'Prince Andrew',
yearBorn: 1960,
royalParent: [Object],
royalChildren: [Array] },
royalChildren: } ]
All I would like to do is extract the objects from the array. (array.length = 0 (-1 + 1?))
Symbols, prototypes, iterables... What I would love is a working example of the method above. Links would be appreciated too.
javascript class oop depth-first-search for-of-loop
I'm trying to wrap my head around ES6 class declaration, instances of a class, the for...of statement, and recursive depth-first traversal of a tree.
Here is my tree:
And here is the construction of the tree:
// let index = Symbol('index');
class Royal {
constructor(name, yearBorn) {
this.name = name;
this.yearBorn = yearBorn;
this.royalParent = null;
// this.royalChildren = ;
this.royalChildren = new Array();
// this[index] = 0;
}
// [Symbol.iterator]() {
// let index = 0;
// return {
// next: () => ({
// value: this.offspring[index++],
// done: index > this.offspring.length
// })
// };
// }
// Adds royal child as a child of this royal parent
addRoyalChild(royalChild) {
// this[this[index]] = royalChild;
// this[index]++;
royalChild.royalParent = this;
this.royalChildren.push(royalChild);
// console.log('this[index]: ', this[index]);
}
// Returns the total number of direct offspring of this royal parent
get numberOfRoyalChildren() {
return `Should be first royal child: ${this.royalChildren}`;
// return this.royalChildren.length;
}
// Returns the number of generations away from the original royal (George V) that this royal is
get numberOfStepsFromOriginal() {
let numberOfSteps = 0;
let currentRoyalChild = this;
while (currentRoyalChild.royalParent) {
currentRoyalChild = currentRoyalChild.royalParent;
numberOfSteps++;
}
return numberOfSteps;
}
// Returns true if this royal is more senior than the other royal. i.e. Who is closer to the original royal?
isMoreSeniorThan(royalChild) {
if ((this.numberOfStepsFromOriginal - royalChild.numberOfStepsFromOriginal) < 0) {
return true;
}
return false;
}
allRoyals() {
let royals = ;
if (Array.isArray(this.royalChildren)) {
royals.push(this);
for (const royalChild of this.royalChildren) {
const royalChildrenArray = royalChild.allRoyals();
royals = royals.concat(royalChildrenArray);
}
} else {
console.log('Oy!');
}
return royals;
}
/** Tree traversal methods **/
// Returns the royal object with that name, or null if no royal exists with that name
royalWithName(name1, name2) {
let royals = new Array();
if (Array.isArray(this.royalChildren)) {
if ((this.name === name1) || (this.name === name2)) {
royals.push(this);
}
for (let royal of this.royalChildren) {
let royalsWithMatchingName = new Array();
royalsWithMatchingName = royal.royalWithName(name1, name2);
royals = royals.concat(royalsWithMatchingName);
}
} else {
return null;
}
return royals;
}
// Returns the number of descendents of a royal
get totalDescendents() {
return this.allRoyals().slice(1).length;
}
}
const georgeV = new Royal('George V', 1865);
const edwardVIII = new Royal('Edward VIII', 1894);
const georgeVI = new Royal('George VI', 1895);
const elizabethII = new Royal('Elizabeth II', 1926);
const margaret = new Royal('Princess Margaret', 1930);
const charles = new Royal('Prince Charles', 1948);
const anne = new Royal('Princess Anne', 1950);
const andrew = new Royal('Prince Andrew', 1960);
const edward = new Royal('Prince Edward', 1964);
const william = new Royal('Prince William', 1982);
const harry = new Royal('Prince Harry', 1984);
const beatrice = new Royal('Princess Beatrice', 1988);
const eugenie = new Royal('Princess Eugenie', 1990);
const louise = new Royal('Lady Louise', 2003);
const severn = new Royal('Viscount Severn', 2007);
georgeV.addRoyalChild(edwardVIII);
georgeV.addRoyalChild(georgeVI);
georgeVI.addRoyalChild(elizabethII);
georgeVI.addRoyalChild(margaret);
elizabethII.addRoyalChild(charles);
elizabethII.addRoyalChild(anne);
elizabethII.addRoyalChild(andrew);
elizabethII.addRoyalChild(edward);
charles.addRoyalChild(william);
charles.addRoyalChild(harry);
andrew.addRoyalChild(beatrice);
andrew.addRoyalChild(eugenie);
edward.addRoyalChild(louise);
edward.addRoyalChild(severn);
console.log('georgeV instanceof Royal:', georgeV instanceof Royal);
console.log('elizabethII.totalDescendents: ', elizabethII.totalDescendents);
console.log('typeof Royal: ', typeof Royal);
console.log('Royal.prototype: ', Royal.prototype);
// console.log('georgeV.constructor: ', georgeV.constructor);
console.log('royal with name Beatrice or Harry: ', georgeV.royalWithName('Princess Beatrice', 'Prince Harry'));
My burning question is this: How do I extract the objects out of the array that is returned from this method:
royalWithName(name1, name2) {
let royals = new Array();
if (Array.isArray(this.royalChildren)) {
if ((this.name === name1) || (this.name === name2)) {
royals.push(this);
}
for (let royal of this.royalChildren) {
let royalsWithMatchingName = new Array();
royalsWithMatchingName = royal.royalWithName(name1, name2);
royals = royals.concat(royalsWithMatchingName);
}
} else {
return null;
}
return royals;
}
This is node:
royal with name Beatrice or Harry: [ Royal {
name: 'Prince Harry',
yearBorn: 1984,
royalParent:
Royal {
name: 'Prince Charles',
yearBorn: 1948,
royalParent: [Object],
royalChildren: [Array] },
royalChildren: },
Royal {
name: 'Princess Beatrice',
yearBorn: 1988,
royalParent:
Royal {
name: 'Prince Andrew',
yearBorn: 1960,
royalParent: [Object],
royalChildren: [Array] },
royalChildren: } ]
All I would like to do is extract the objects from the array. (array.length = 0 (-1 + 1?))
Symbols, prototypes, iterables... What I would love is a working example of the method above. Links would be appreciated too.
// let index = Symbol('index');
class Royal {
constructor(name, yearBorn) {
this.name = name;
this.yearBorn = yearBorn;
this.royalParent = null;
// this.royalChildren = ;
this.royalChildren = new Array();
// this[index] = 0;
}
// [Symbol.iterator]() {
// let index = 0;
// return {
// next: () => ({
// value: this.offspring[index++],
// done: index > this.offspring.length
// })
// };
// }
// Adds royal child as a child of this royal parent
addRoyalChild(royalChild) {
// this[this[index]] = royalChild;
// this[index]++;
royalChild.royalParent = this;
this.royalChildren.push(royalChild);
// console.log('this[index]: ', this[index]);
}
// Returns the total number of direct offspring of this royal parent
get numberOfRoyalChildren() {
return `Should be first royal child: ${this.royalChildren}`;
// return this.royalChildren.length;
}
// Returns the number of generations away from the original royal (George V) that this royal is
get numberOfStepsFromOriginal() {
let numberOfSteps = 0;
let currentRoyalChild = this;
while (currentRoyalChild.royalParent) {
currentRoyalChild = currentRoyalChild.royalParent;
numberOfSteps++;
}
return numberOfSteps;
}
// Returns true if this royal is more senior than the other royal. i.e. Who is closer to the original royal?
isMoreSeniorThan(royalChild) {
if ((this.numberOfStepsFromOriginal - royalChild.numberOfStepsFromOriginal) < 0) {
return true;
}
return false;
}
allRoyals() {
let royals = ;
if (Array.isArray(this.royalChildren)) {
royals.push(this);
for (const royalChild of this.royalChildren) {
const royalChildrenArray = royalChild.allRoyals();
royals = royals.concat(royalChildrenArray);
}
} else {
console.log('Oy!');
}
return royals;
}
/** Tree traversal methods **/
// Returns the royal object with that name, or null if no royal exists with that name
royalWithName(name1, name2) {
let royals = new Array();
if (Array.isArray(this.royalChildren)) {
if ((this.name === name1) || (this.name === name2)) {
royals.push(this);
}
for (let royal of this.royalChildren) {
let royalsWithMatchingName = new Array();
royalsWithMatchingName = royal.royalWithName(name1, name2);
royals = royals.concat(royalsWithMatchingName);
}
} else {
return null;
}
return royals;
}
// Returns the number of descendents of a royal
get totalDescendents() {
return this.allRoyals().slice(1).length;
}
}
const georgeV = new Royal('George V', 1865);
const edwardVIII = new Royal('Edward VIII', 1894);
const georgeVI = new Royal('George VI', 1895);
const elizabethII = new Royal('Elizabeth II', 1926);
const margaret = new Royal('Princess Margaret', 1930);
const charles = new Royal('Prince Charles', 1948);
const anne = new Royal('Princess Anne', 1950);
const andrew = new Royal('Prince Andrew', 1960);
const edward = new Royal('Prince Edward', 1964);
const william = new Royal('Prince William', 1982);
const harry = new Royal('Prince Harry', 1984);
const beatrice = new Royal('Princess Beatrice', 1988);
const eugenie = new Royal('Princess Eugenie', 1990);
const louise = new Royal('Lady Louise', 2003);
const severn = new Royal('Viscount Severn', 2007);
georgeV.addRoyalChild(edwardVIII);
georgeV.addRoyalChild(georgeVI);
georgeVI.addRoyalChild(elizabethII);
georgeVI.addRoyalChild(margaret);
elizabethII.addRoyalChild(charles);
elizabethII.addRoyalChild(anne);
elizabethII.addRoyalChild(andrew);
elizabethII.addRoyalChild(edward);
charles.addRoyalChild(william);
charles.addRoyalChild(harry);
andrew.addRoyalChild(beatrice);
andrew.addRoyalChild(eugenie);
edward.addRoyalChild(louise);
edward.addRoyalChild(severn);
console.log('georgeV instanceof Royal:', georgeV instanceof Royal);
console.log('elizabethII.totalDescendents: ', elizabethII.totalDescendents);
console.log('typeof Royal: ', typeof Royal);
console.log('Royal.prototype: ', Royal.prototype);
// console.log('georgeV.constructor: ', georgeV.constructor);
console.log('royal with name Beatrice or Harry: ', georgeV.royalWithName('Princess Beatrice', 'Prince Harry'));
// let index = Symbol('index');
class Royal {
constructor(name, yearBorn) {
this.name = name;
this.yearBorn = yearBorn;
this.royalParent = null;
// this.royalChildren = ;
this.royalChildren = new Array();
// this[index] = 0;
}
// [Symbol.iterator]() {
// let index = 0;
// return {
// next: () => ({
// value: this.offspring[index++],
// done: index > this.offspring.length
// })
// };
// }
// Adds royal child as a child of this royal parent
addRoyalChild(royalChild) {
// this[this[index]] = royalChild;
// this[index]++;
royalChild.royalParent = this;
this.royalChildren.push(royalChild);
// console.log('this[index]: ', this[index]);
}
// Returns the total number of direct offspring of this royal parent
get numberOfRoyalChildren() {
return `Should be first royal child: ${this.royalChildren}`;
// return this.royalChildren.length;
}
// Returns the number of generations away from the original royal (George V) that this royal is
get numberOfStepsFromOriginal() {
let numberOfSteps = 0;
let currentRoyalChild = this;
while (currentRoyalChild.royalParent) {
currentRoyalChild = currentRoyalChild.royalParent;
numberOfSteps++;
}
return numberOfSteps;
}
// Returns true if this royal is more senior than the other royal. i.e. Who is closer to the original royal?
isMoreSeniorThan(royalChild) {
if ((this.numberOfStepsFromOriginal - royalChild.numberOfStepsFromOriginal) < 0) {
return true;
}
return false;
}
allRoyals() {
let royals = ;
if (Array.isArray(this.royalChildren)) {
royals.push(this);
for (const royalChild of this.royalChildren) {
const royalChildrenArray = royalChild.allRoyals();
royals = royals.concat(royalChildrenArray);
}
} else {
console.log('Oy!');
}
return royals;
}
/** Tree traversal methods **/
// Returns the royal object with that name, or null if no royal exists with that name
royalWithName(name1, name2) {
let royals = new Array();
if (Array.isArray(this.royalChildren)) {
if ((this.name === name1) || (this.name === name2)) {
royals.push(this);
}
for (let royal of this.royalChildren) {
let royalsWithMatchingName = new Array();
royalsWithMatchingName = royal.royalWithName(name1, name2);
royals = royals.concat(royalsWithMatchingName);
}
} else {
return null;
}
return royals;
}
// Returns the number of descendents of a royal
get totalDescendents() {
return this.allRoyals().slice(1).length;
}
}
const georgeV = new Royal('George V', 1865);
const edwardVIII = new Royal('Edward VIII', 1894);
const georgeVI = new Royal('George VI', 1895);
const elizabethII = new Royal('Elizabeth II', 1926);
const margaret = new Royal('Princess Margaret', 1930);
const charles = new Royal('Prince Charles', 1948);
const anne = new Royal('Princess Anne', 1950);
const andrew = new Royal('Prince Andrew', 1960);
const edward = new Royal('Prince Edward', 1964);
const william = new Royal('Prince William', 1982);
const harry = new Royal('Prince Harry', 1984);
const beatrice = new Royal('Princess Beatrice', 1988);
const eugenie = new Royal('Princess Eugenie', 1990);
const louise = new Royal('Lady Louise', 2003);
const severn = new Royal('Viscount Severn', 2007);
georgeV.addRoyalChild(edwardVIII);
georgeV.addRoyalChild(georgeVI);
georgeVI.addRoyalChild(elizabethII);
georgeVI.addRoyalChild(margaret);
elizabethII.addRoyalChild(charles);
elizabethII.addRoyalChild(anne);
elizabethII.addRoyalChild(andrew);
elizabethII.addRoyalChild(edward);
charles.addRoyalChild(william);
charles.addRoyalChild(harry);
andrew.addRoyalChild(beatrice);
andrew.addRoyalChild(eugenie);
edward.addRoyalChild(louise);
edward.addRoyalChild(severn);
console.log('georgeV instanceof Royal:', georgeV instanceof Royal);
console.log('elizabethII.totalDescendents: ', elizabethII.totalDescendents);
console.log('typeof Royal: ', typeof Royal);
console.log('Royal.prototype: ', Royal.prototype);
// console.log('georgeV.constructor: ', georgeV.constructor);
console.log('royal with name Beatrice or Harry: ', georgeV.royalWithName('Princess Beatrice', 'Prince Harry'));
javascript class oop depth-first-search for-of-loop
javascript class oop depth-first-search for-of-loop
edited Nov 7 at 18:28
ggorlen
5,8163825
5,8163825
asked Nov 7 at 18:18
cocomatt
337
337
1
I'm not entirely clear what you mean by "All I would like to do is extract the objects from the array." What is your expected output?
– ggorlen
Nov 7 at 18:30
I can get the array. With name1 and name2 there are two objects inside the array. Console.log works. I want to return royal object[0] or maybe [1].
– cocomatt
Nov 7 at 18:36
1
array[0]
orarray[1]
isn't working for that?
– ggorlen
Nov 7 at 18:42
console.log(array[0]) works; return array[0] doesn't
– cocomatt
Nov 7 at 18:43
add a comment |
1
I'm not entirely clear what you mean by "All I would like to do is extract the objects from the array." What is your expected output?
– ggorlen
Nov 7 at 18:30
I can get the array. With name1 and name2 there are two objects inside the array. Console.log works. I want to return royal object[0] or maybe [1].
– cocomatt
Nov 7 at 18:36
1
array[0]
orarray[1]
isn't working for that?
– ggorlen
Nov 7 at 18:42
console.log(array[0]) works; return array[0] doesn't
– cocomatt
Nov 7 at 18:43
1
1
I'm not entirely clear what you mean by "All I would like to do is extract the objects from the array." What is your expected output?
– ggorlen
Nov 7 at 18:30
I'm not entirely clear what you mean by "All I would like to do is extract the objects from the array." What is your expected output?
– ggorlen
Nov 7 at 18:30
I can get the array. With name1 and name2 there are two objects inside the array. Console.log works. I want to return royal object[0] or maybe [1].
– cocomatt
Nov 7 at 18:36
I can get the array. With name1 and name2 there are two objects inside the array. Console.log works. I want to return royal object[0] or maybe [1].
– cocomatt
Nov 7 at 18:36
1
1
array[0]
or array[1]
isn't working for that?– ggorlen
Nov 7 at 18:42
array[0]
or array[1]
isn't working for that?– ggorlen
Nov 7 at 18:42
console.log(array[0]) works; return array[0] doesn't
– cocomatt
Nov 7 at 18:43
console.log(array[0]) works; return array[0] doesn't
– cocomatt
Nov 7 at 18:43
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53195465%2ffor-of-statement-depth-first-traversal-of-a-tree-javascript%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
I'm not entirely clear what you mean by "All I would like to do is extract the objects from the array." What is your expected output?
– ggorlen
Nov 7 at 18:30
I can get the array. With name1 and name2 there are two objects inside the array. Console.log works. I want to return royal object[0] or maybe [1].
– cocomatt
Nov 7 at 18:36
1
array[0]
orarray[1]
isn't working for that?– ggorlen
Nov 7 at 18:42
console.log(array[0]) works; return array[0] doesn't
– cocomatt
Nov 7 at 18:43