From 4a7eddc14f2aa5e97f09796706eb9b69fc4a5bdb Mon Sep 17 00:00:00 2001 From: Anna Yasenova Date: Sun, 5 Mar 2017 23:04:29 +0200 Subject: [PATCH 1/2] Additional functions. --- JavaScript/4-newfunc.js | 78 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 JavaScript/4-newfunc.js diff --git a/JavaScript/4-newfunc.js b/JavaScript/4-newfunc.js new file mode 100644 index 0000000..912956e --- /dev/null +++ b/JavaScript/4-newfunc.js @@ -0,0 +1,78 @@ +'use strict'; + +function Node(parent, name, data) { + this.name = name; + this.data = data; + if (parent) { + this.parent = parent; + parent[name] = this; + } + this.child = []; +} + +function Tree(name, data) { + this.root = new Node(null, name, data); +} + +Tree.prototype.visitDepth = function(callback) { + + (function recursive(currNode) { + + let len = currNode.child.length, + num; + for (num = 0; num < len; ++num) { + recursive(currNode.child[num]); + } + + callback(currNode); + })(this.root); +}; + +Tree.prototype.isHave = function(callback) { + this.visitDepth.call(this, callback); +}; + +Tree.prototype.addData = function(name, data, whereAdd) { + let parent = null, + callback = function(n) { + if (n.name === whereAdd) { + parent = n; + } + }; + this.isHave(callback); + + let node = new Node(parent, name, data); + if (parent) { + parent.child.push(node); + node.parent = parent; + } else { + throw new Error('Parent not exist!'); + } +}; + + +let tree = new Tree('one', 1); + +tree.root.child.push(new Node(tree, 'two', 2)); + +tree.root.child.push(new Node(tree, 'three', 3)); + +tree.root.child.push(new Node(tree, 'four', 4)); + +tree.root.child[0].child.push(new Node(tree.root.child[0], 'five', 5)); + +tree.root.child[0].child.push(new Node(tree.root.child[0], 'six', 6)); + +tree.root.child[2].child.push(new Node(tree.root.child[2], 'seven', 7)); + + +tree.isHave(n => { + if (n.name === 'five') { + console.dir(n); + } +}); + +tree.visitDepth(n => console.dir(n)); + +tree.addData('qwe', 456, 'two'); +console.dir(tree); From bc07369bb77f1a06200d1589304b8b316996015d Mon Sep 17 00:00:00 2001 From: Anna Yasenova Date: Mon, 6 Mar 2017 15:52:02 +0200 Subject: [PATCH 2/2] Used 'const' instead of 'let'. Eslint fixed. --- JavaScript/4-newfunc.js | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/JavaScript/4-newfunc.js b/JavaScript/4-newfunc.js index 912956e..d864d7a 100644 --- a/JavaScript/4-newfunc.js +++ b/JavaScript/4-newfunc.js @@ -18,8 +18,8 @@ Tree.prototype.visitDepth = function(callback) { (function recursive(currNode) { - let len = currNode.child.length, - num; + const len = currNode.child.length; + let num; for (num = 0; num < len; ++num) { recursive(currNode.child[num]); } @@ -33,15 +33,15 @@ Tree.prototype.isHave = function(callback) { }; Tree.prototype.addData = function(name, data, whereAdd) { - let parent = null, - callback = function(n) { - if (n.name === whereAdd) { - parent = n; - } - }; + let parent = null; + const callback = function(n) { + if (n.name === whereAdd) { + parent = n; + } + }; this.isHave(callback); - let node = new Node(parent, name, data); + const node = new Node(parent, name, data); if (parent) { parent.child.push(node); node.parent = parent; @@ -51,7 +51,7 @@ Tree.prototype.addData = function(name, data, whereAdd) { }; -let tree = new Tree('one', 1); +const tree = new Tree('one', 1); tree.root.child.push(new Node(tree, 'two', 2));