From 0b21ca393cc694ad70deb963c0cfdf2e77f52fd6 Mon Sep 17 00:00:00 2001 From: Evgeniy Burlachenko Date: Sat, 4 Mar 2017 22:08:17 +0200 Subject: [PATCH 1/2] Implemented tree using classes, additional functionality --- JavaScript/4-class.js | 58 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 JavaScript/4-class.js diff --git a/JavaScript/4-class.js b/JavaScript/4-class.js new file mode 100644 index 0000000..b86283b --- /dev/null +++ b/JavaScript/4-class.js @@ -0,0 +1,58 @@ +'use strict'; + +class Node { + + constructor(parent, name, data) { + this.name = name; + this.data = data; + this.childs = []; + if (parent) { + this.parent = parent; + parent.childs.push(this); + } + } + + findAll(name) { // Find all nodes with specified name + let arr = []; + if (this.name === name) arr.push(this); + for (let n of this.childs) { + arr = arr.concat(n.findAll(name)); + } + + return arr; + } + + findFirst(name) { //Find first instance + if (this.name === name) return this; + for (let n of this.childs) { + return n.findFirst(name); + } + } + + find(name, callback) { //Call function for each found node + const nodes = this.findAll(name); + if (nodes.length > 0) + for (let n of this.findAll(name)) + callback(n); + } + + setParent(newParent) { // Sets new parent for this node + this.parent.childs.pop(this.parent.childs.indexOf(this)); + this.parent = newParent; + this.parent.childs.push(this); + } +} + +const root = new Node(null, 'root', {}); +const n1 = new Node(root, 'searched', {}); +const n2 = new Node(root, 'n2', {}); +const n12 = new Node(n1, 'searched', {}); + + +console.log('findAll("searched") -', root.findAll('searched')); +console.log('findFirst("searched") -', root.findFirst('searched')); +root.find('n2', n => { + console.log('parent - ', n.parent); +}); +n12.setParent(root); +console.log('n12 new parent -', n12.parent); From 4aa24fb6ff9eb5f287906ebd85b4daea104a1631 Mon Sep 17 00:00:00 2001 From: Evgeniy Burlachenko Date: Sun, 5 Mar 2017 00:09:12 +0200 Subject: [PATCH 2/2] Code review related fixes --- JavaScript/4-class.js | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/JavaScript/4-class.js b/JavaScript/4-class.js index b86283b..3948604 100644 --- a/JavaScript/4-class.js +++ b/JavaScript/4-class.js @@ -13,10 +13,11 @@ class Node { } findAll(name) { // Find all nodes with specified name - let arr = []; + const arr = []; if (this.name === name) arr.push(this); - for (let n of this.childs) { - arr = arr.concat(n.findAll(name)); + let i; + for (i = 0; i < this.childs.length; i++) { + arr.push.apply(arr, this.childs[i].findAll(name)); } return arr; @@ -24,20 +25,25 @@ class Node { findFirst(name) { //Find first instance if (this.name === name) return this; - for (let n of this.childs) { - return n.findFirst(name); + let i; + for (i = 0; i < this.childs.length; i++) { + return this.childs[i].findFirst(name); } } find(name, callback) { //Call function for each found node const nodes = this.findAll(name); - if (nodes.length > 0) - for (let n of this.findAll(name)) - callback(n); + if (nodes.length > 0) { + let i; + const found = this.findAll(name); + for (i = 0; i < found.length; i++) { + callback(found[i]); + } + } } setParent(newParent) { // Sets new parent for this node - this.parent.childs.pop(this.parent.childs.indexOf(this)); + this.parent.childs.pop(this.parent.childs.indexOf(this)); this.parent = newParent; this.parent.childs.push(this); } @@ -51,8 +57,6 @@ const n12 = new Node(n1, 'searched', {}); console.log('findAll("searched") -', root.findAll('searched')); console.log('findFirst("searched") -', root.findFirst('searched')); -root.find('n2', n => { - console.log('parent - ', n.parent); -}); +root.find('n2', n => console.log('parent - ', n.parent)); n12.setParent(root); console.log('n12 new parent -', n12.parent); pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy