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:



The house of Windsor



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.










share|improve this question




















  • 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] 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

















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:



The house of Windsor



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.










share|improve this question




















  • 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] 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















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:



The house of Windsor



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.










share|improve this question















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:



The house of Windsor



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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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] 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
















  • 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] 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










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



















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















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






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















 

draft saved


draft discarded



















































 


draft saved


draft discarded














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





















































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







這個網誌中的熱門文章

Hercules Kyvelos

Tangent Lines Diagram Along Smooth Curve

Yusuf al-Mu'taman ibn Hud