Best JavaScript code snippet using wpt
tree.ts
Source:tree.ts
1console.log("ð³ð³ð³ð³");2class TreeNode {3 val: any;4 right: TreeNode;5 left: TreeNode;6 count: number;7 constructor(val: any) {8 this.val = val;9 this.right = null;10 this.left = null;11 this.count = 0;12 }13}14class BST {15 root: TreeNode;16 constructor() {17 this.root = null;18 }19 create(val) {20 const newNode = new TreeNode(val);21 if (!this.root) {22 this.root = newNode;23 return this;24 }25 let current = this.root;26 const addSide = (side) => {27 if (!current[side]) {28 current[side] = newNode;29 return this;30 }31 current = current[side];32 };33 while (true) {34 if (val === current.val) {35 current.count++;36 return this;37 }38 if (val < current.val) addSide("left");39 else addSide("right");40 }41 }42 // execute breadth-first search43 breadthFirstSearch(start?: any) {44 let search: TreeNode | boolean = this.find(start);45 let visited = [];46 let queue = [];47 let current: TreeNode = search instanceof TreeNode ? search : this.root;48 queue.push(current);49 while (queue.length) {50 current = queue.shift();51 visited.push(current.val);52 if (current && current.left) queue.push(current.left);53 if (current.right) queue.push(current.right);54 }55 return visited;56 }57 //Depth-first searches: preOrder, postOrder, inOrder58 preOrderDepthSearch() {59 let visited = [];60 let current = this.root;61 let traverse = (node) => {62 visited.push(node.val);63 if (node.left) traverse(node.left);64 if (node.right) traverse(node.right);65 };66 traverse(current);67 return visited;68 }69 postOrderDepthSearch() {70 let visited = [];71 let current = this.root;72 let traverse = (node) => {73 if (node.left) {74 traverse(node.left);75 }76 if (node.right) {77 traverse(node.right);78 }79 visited.push(node.val);80 };81 traverse(current);82 return visited;83 }84 inOrderDepthSearch() {85 let visited = [];86 let current = this.root;87 let traverse = (node) => {88 if (node.left) {89 traverse(node.left);90 }91 visited.push(node.val);92 if (node.right) {93 traverse(node.right);94 }95 };96 traverse(current);97 return visited;98 }99 find(val: any): TreeNode | boolean {100 if (!this.root) return undefined;101 let current: TreeNode = this.root;102 let found: boolean = false;103 while (current && !found) {104 if (val < current.val) current = current.left;105 else if (val > current.val) current = current.right;106 else found = true;107 }108 if (!found) return false;109 return current;110 }111 delete(val) {112 if (!this.root) return undefined;113 let current = this.root;114 let parent;115 const pickSide = (side) => {116 if (!current[side]) return "no node found!";117 parent = current;118 current = current[side];119 };120 const deleteNode = (side) => {121 if (current.val === val && current.count > 1) current.count--;122 else if (current.val === val) {123 const children = this.breadthFirstSearch(current.val);124 parent[side] = null;125 children.splice(0, 1);126 children.forEach((child) => this.create(child));127 }128 };129 while (current.val !== val) {130 if (val < current.val) {131 pickSide("left");132 deleteNode("left");133 } else {134 pickSide("right");135 deleteNode("right");136 }137 }138 }139}140const tree = new BST();141tree.create(20);142tree.create(14);143tree.create(57);144tree.create(9);145tree.create(19);146tree.create(31);147tree.create(62);148tree.create(3);149tree.create(11);150tree.create(72);151// console.log(tree.BFS());152// tree.preOrder();153// tree.postOrder();154// tree.inOrder();155const nameTree = new BST();156nameTree.create("floyd");157nameTree.create("bob");158nameTree.create("doug");159nameTree.create("alan");160nameTree.create("gregor");161nameTree.create("egan");162nameTree.create("clarence");163nameTree.create("zepher");164const bredth = nameTree.breadthFirstSearch();165const pre = nameTree.preOrderDepthSearch();166const post = nameTree.postOrderDepthSearch();167const inOrder = nameTree.inOrderDepthSearch();168// console.log(nameTree.breadthFirstSearch());169nameTree.delete("zepher");170// console.log(nameTree.breadthFirstSearch());171// console.log({ nameTree });172// console.log({ bredth });173// console.log({ pre });174// console.log({ post });175// console.log({ inOrder });176// console.log(nameTree.find("bob"));177// console.log(tree);178// const factorial = (n) => {179// let num = n;180// if (n === 0) return 1;181// for (let i = 0; i < n; i++) {182// console.log(num);183// num = n * factorial(n - 1);184// }185// return num;186// };187// console.log(factorial(12));188function miniMaxSum(array: number[]): void {189 let min: number;190 let max: number;191 let possibleAnswers: number[] = [];192 let answer: string = `${min} ${max}`;193 for (let i = 0; i < array.length; i++) {194 console.log(i);195 // const tempArray: number[] = array.filter((number, j) => {196 // console.log(i,j);197 // if (i != j) {198 // console.log(number);199 // return number200 // }201 // })202 // possibleAnswers.push(tempArray.reduce((a,b) => {return a+b}, 0))203 }204 console.log(possibleAnswers);205}...
NameHierarchicalTree.spec.js
Source:NameHierarchicalTree.spec.js
1const chai = require("chai");2const expect = chai.expect;3chai.should();4const NameHierarchicalTree = require("./../src/NameHierarchicalTree");5const TreeNode = require("./../src/TreeNode");6describe("NameHierarchicalTree.js", function () {7 it("Should exist", function () {8 expect(NameHierarchicalTree).to.exist;9 });10 describe("_searchNode", function () {11 it("Should return 0", function () {12 let nameTree = new NameHierarchicalTree();13 nameTree.getChildrenOf("testErrorCode").should.equal(0);14 });15 it("Should return the node given the name", function () {16 let nameTree = new NameHierarchicalTree();17 let treeNode1 = new TreeNode("parent");18 let treeNode2 = new TreeNode("parent.child1");19 let treeNode3 = new TreeNode("parent.child2");20 treeNode1.addChild(treeNode2);21 treeNode1.addChild(treeNode3);22 nameTree.head.addChild(treeNode1);23 nameTree._searchNode("parent").name.should.equal("parent");24 nameTree25 ._searchNode("parent.child1")26 .name.should.equal("parent.child1");27 nameTree28 ._searchNode("parent.child2")29 .name.should.equal("parent.child2");30 });31 });32 describe("addChild", function () {33 it("Should return 1", function () {34 let nameTree = new NameHierarchicalTree();35 nameTree.addChild("parent");36 nameTree.addChild("testErrorCode.1").should.equal(1);37 });38 it("Should insert succesfully", function () {39 let nameTree = new NameHierarchicalTree();40 nameTree.addChild("parent");41 nameTree.addChild("parent.child1");42 nameTree.addChild("parent.child2");43 nameTree.addChild("parent.child1.child1-1");44 nameTree.head.children[0].name.should.equal("parent");45 nameTree.head.children[0].children[0].name.should.equal("parent.child1");46 nameTree.head.children[0].children[1].name.should.equal("parent.child2");47 nameTree.head.children[0].children[0].children[0].name.should.equal(48 "parent.child1.child1-1"49 );50 });51 it("Should insert succesfully", function () {52 let nameTree = new NameHierarchicalTree("@");53 nameTree.addChild("parent");54 nameTree.addChild("parent@child1");55 nameTree.addChild("parent@child2");56 nameTree.addChild("parent@child1@child1-1");57 nameTree.head.children[0].name.should.equal("parent");58 nameTree.head.children[0].children[0].name.should.equal("parent@child1");59 nameTree.head.children[0].children[1].name.should.equal("parent@child2");60 nameTree.head.children[0].children[0].children[0].name.should.equal(61 "parent@child1@child1-1"62 );63 });64 it("Should insert succesfully", function () {65 let nameTree = new NameHierarchicalTree("/");66 nameTree.addChild("parent");67 nameTree.addChild("parent/child1");68 nameTree.addChild("parent/child2");69 nameTree.addChild("parent/child1/child1-1");70 nameTree.head.children[0].name.should.equal("parent");71 nameTree.head.children[0].children[0].name.should.equal("parent/child1");72 nameTree.head.children[0].children[1].name.should.equal("parent/child2");73 nameTree.head.children[0].children[0].children[0].name.should.equal(74 "parent/child1/child1-1"75 );76 });77 });78 describe("getChildrenOf", function () {79 it("Should return 0", function () {80 let nameTree = new NameHierarchicalTree();81 nameTree.addChild("parent");82 nameTree.addChild("parent.child1");83 nameTree.addChild("parent.child2");84 nameTree.getChildrenOf("testErrorCode").should.equal(0);85 }),86 it("Should return the children of the node", function () {87 let nameTree = new NameHierarchicalTree();88 nameTree.addChild("parent");89 nameTree.addChild("parent.child1");90 nameTree.addChild("parent.child2");91 nameTree.addChild("parent.child1.child1-1");92 nameTree.getChildrenOf("parent")[0].name.should.equal("parent.child1");93 nameTree.getChildrenOf("parent")[1].name.should.equal("parent.child2");94 nameTree95 .getChildrenOf("parent.child1")[0]96 .name.should.equal("parent.child1.child1-1");97 });98 });99 describe("removeChild", function () {100 it("Should return 1", function () {101 let nameTree = new NameHierarchicalTree();102 nameTree.addChild("parent");103 nameTree.addChild("parent.child1");104 nameTree.removeChild("testError").should.equal(1);105 });106 it("Should remove succesfully", function () {107 let nameTree = new NameHierarchicalTree();108 nameTree.addChild("parent");109 nameTree.addChild("parent.child1");110 nameTree.addChild("parent.child2");111 nameTree.addChild("parent.child1.child1-1");112 nameTree.removeChild("parent.child1.child1-1");113 nameTree.getChildrenOf("parent.child1").should.be.empty;114 nameTree.removeChild("parent");115 nameTree._searchNode("parent").should.equal(0);116 });117 });...
macroDirectory.js
Source:macroDirectory.js
1(function() {2 var self = this;3 var $class, _;4 $class = require("./class").class;5 _ = require("underscore");6 module.exports = function(terms) {7 var self = this;8 var macroDirectory, createMacroDirectory;9 macroDirectory = $class({10 constructor: function() {11 var self = this;12 return self.nameTreeRoot = {};13 },14 nameNode: function(name) {15 var self = this;16 var nameTree;17 nameTree = self.nameTreeRoot;18 _(name).each(function(nameSegment) {19 if (!nameTree.hasOwnProperty(nameSegment)) {20 return nameTree = nameTree[nameSegment] = {};21 } else {22 return nameTree = nameTree[nameSegment];23 }24 });25 return nameTree;26 },27 addMacro: function(name, createMacro) {28 var self = this;29 var nameTree;30 nameTree = self.nameNode(name);31 return nameTree["create macro"] = createMacro;32 },33 addWildCardMacro: function(name, matchMacro) {34 var self = this;35 var nameTree, matchMacros;36 nameTree = self.nameNode(name);37 matchMacros = void 0;38 if (!nameTree.hasOwnProperty("match macro")) {39 matchMacros = nameTree["match macro"] = [];40 } else {41 matchMacros = nameTree["match macro"];42 }43 return matchMacros.push(matchMacro);44 },45 findMacro: function(name) {46 var self = this;47 var findMatchingWildMacro, findMacroInTree;48 findMatchingWildMacro = function(wildMacros, name) {49 var n, wildMacro, macro;50 n = 0;51 while (n < wildMacros.length) {52 wildMacro = wildMacros[n];53 macro = wildMacro(name);54 if (macro) {55 return macro;56 }57 ++n;58 }59 return void 0;60 };61 findMacroInTree = function(nameTree, name, index, wildMacros) {62 var subtree;63 if (index < name.length) {64 if (nameTree.hasOwnProperty(name[index])) {65 subtree = nameTree[name[index]];66 if (subtree.hasOwnProperty("match macro")) {67 wildMacros = subtree["match macro"].concat(wildMacros);68 }69 return findMacroInTree(subtree, name, index + 1, wildMacros);70 } else {71 return findMatchingWildMacro(wildMacros, name);72 }73 } else {74 if (nameTree.hasOwnProperty("create macro")) {75 return nameTree["create macro"];76 } else {77 return findMatchingWildMacro(wildMacros, name);78 }79 }80 };81 return findMacroInTree(self.nameTreeRoot, name, 0, []);82 }83 });84 return createMacroDirectory = function() {85 var args = Array.prototype.slice.call(arguments, 0, arguments.length);86 var gen1_c;87 gen1_c = function() {88 macroDirectory.apply(this, args);89 };90 gen1_c.prototype = macroDirectory.prototype;91 return new gen1_c();92 };93 };...
Using AI Code Generation
1var tree = new NameTree();2tree.add("a", "a");3tree.add("b", "b");4tree.add("c", "c");5tree.add("d", "d");6tree.add("e", "e");7tree.add("f", "f");8tree.add("g", "g");9tree.add("h", "h");10tree.add("i", "i");11tree.add("j", "j");12tree.add("k", "k");13tree.add("l", "l");14tree.add("m", "m");15tree.add("n", "n");16tree.add("o", "o");17tree.add("p", "p");18tree.add("q", "q");19tree.add("r", "r");20tree.add("s", "s");21tree.add("t", "t");22tree.add("u", "u");23tree.add("v", "v");24tree.add("w", "w");25tree.add("x", "x");26tree.add("y", "y");27tree.add("z", "z");28tree.add("aa", "aa");29tree.add("ab", "ab");30tree.add("ac", "ac");31tree.add("ad", "ad");32tree.add("ae", "ae");33tree.add("af", "af");34tree.add("ag", "ag");35tree.add("ah", "ah");36tree.add("ai", "ai");37tree.add("aj", "aj");38tree.add("ak", "ak");39tree.add("al", "al");40tree.add("am", "am");41tree.add("an", "an");42tree.add("ao", "ao");43tree.add("ap", "ap");44tree.add("aq", "aq");45tree.add("ar", "ar");46tree.add("as", "as");47tree.add("at", "at");48tree.add("au", "au");49tree.add("av", "av");50tree.add("aw", "aw");51tree.add("ax", "ax");52tree.add("ay", "ay");53tree.add("az", "az");54tree.add("ba", "ba");55tree.add("bb", "bb");56tree.add("bc", "bc");57tree.add("bd", "bd");58tree.add("be", "be");59tree.add("bf", "bf");60tree.add("bg", "bg");61tree.add("bh", "bh");62tree.add("bi", "bi");
Using AI Code Generation
1var wptree = require('./wptree.js');2var nt = new wptree.NameTree();3nt.add('a', 1);4nt.add('b', 2);5nt.add('c', 3);6nt.add('d', 4);7nt.add('e', 5);8nt.add('f', 6);9nt.add('g', 7);10nt.add('h', 8);11nt.add('i', 9);12nt.add('j', 10);13nt.add('k', 11);14nt.add('l', 12);15nt.add('m', 13);16nt.add('n', 14);17nt.add('o', 15);18nt.add('p', 16);19nt.add('q', 17);20nt.add('r', 18);21nt.add('s', 19);22nt.add('t', 20);23nt.add('u', 21);24nt.add('v', 22);25nt.add('w', 23);26nt.add('x', 24);27nt.add('y', 25);28nt.add('z', 26);29nt.add('aa', 27);30nt.add('ab', 28);31nt.add('ac', 29);32nt.add('ad', 30);33nt.add('ae', 31);34nt.add('af', 32);35nt.add('ag', 33);36nt.add('ah', 34);37nt.add('ai', 35);38nt.add('aj', 36);39nt.add('ak', 37);40nt.add('al', 38);41nt.add('am', 39);42nt.add('an', 40);43nt.add('ao', 41);44nt.add('ap', 42);45nt.add('aq', 43);46nt.add('ar', 44);47nt.add('as', 45);48nt.add('at', 46);49nt.add('au', 47);50nt.add('av', 48);51nt.add('aw', 49);52nt.add('ax', 50);53nt.add('ay', 51);54nt.add('az', 52);55nt.add('ba', 53);56nt.add('bb', 54);57nt.add('bc', 55);58nt.add('bd', 56);59nt.add('be', 57);60nt.add('bf', 58);61nt.add('bg', 59);
Using AI Code Generation
1var fs = require('fs');2var wptree = require('./wptree.js');3var tree = new wptree.NameTree();4var data = fs.readFileSync('names.txt', 'utf8');5var names = data.split('6');7names.forEach(function(name) {8 tree.add(name);9});10console.log(tree.count());11console.log(tree.count('M'));12console.log(tree.count('M', 'M'));13console.log(tree.count('M', 'N'));14console.log(tree.count('M', 'O'));15console.log(tree.count('N'));16console.log(tree.count('N', 'M'));17console.log(tree.count('N', 'N'));18console.log(tree.count('N', 'O'));19console.log(tree.count('O'));20console.log(tree.count('O', 'M'));21console.log(tree.count('O', 'N'));22console.log(tree.count('O', 'O'));
Using AI Code Generation
1const wptree = require('./wptree.js');2let tree = new wptree.NameTree();3tree.insert('John');4tree.insert('Paul');5tree.insert('George');6tree.insert('Ringo');7tree.insert('Pete');8tree.insert('Stuart');9tree.insert('Andy');10tree.insert('Mick');11tree.insert('Keith');12tree.insert('Ronnie');13tree.insert('Charlie');14tree.insert('Bill');15tree.insert('Derek');16tree.insert('Brian');17tree.insert('Phil');18tree.insert('Eric');19tree.insert('Tony');20tree.insert('Mick');21tree.insert('Ronnie');22tree.insert('Charlie');23tree.insert('Bill');24tree.insert('Derek');25tree.insert('Brian');26tree.insert('Phil');27tree.insert('Eric');28tree.insert('Tony');29tree.insert('Mick');30tree.insert('Ronnie');31tree.insert('Charlie');32tree.insert('Bill');33tree.insert('Derek');34tree.insert('Brian');35tree.insert('Phil');36tree.insert('Eric');37tree.insert('Tony');38tree.insert('Mick');39tree.insert('Ronnie');40tree.insert('Charlie');41tree.insert('Bill');42tree.insert('Derek');43tree.insert('Brian');44tree.insert('Phil');45tree.insert('Eric');46tree.insert('Tony');47tree.insert('Mick');48tree.insert('Ronnie');49tree.insert('Charlie');50tree.insert('Bill');51tree.insert('Derek');52tree.insert('Brian');53tree.insert('Phil');54tree.insert('Eric');55tree.insert('Tony');56tree.insert('Mick');57tree.insert('Ronnie');58tree.insert('Charlie');59tree.insert('Bill');60tree.insert('Derek');61tree.insert('Brian');62tree.insert('Phil');63tree.insert('Eric');64tree.insert('Tony');65tree.insert('Mick');66tree.insert('Ronnie');67tree.insert('Charlie');68tree.insert('Bill');69tree.insert('Derek');70tree.insert('Brian');71tree.insert('Phil');72tree.insert('Eric');73tree.insert('Tony');74tree.insert('Mick');75tree.insert('Ronnie');76tree.insert('Charlie');77tree.insert('Bill');78tree.insert('Derek');79tree.insert('Brian');80tree.insert('Phil');81tree.insert('Eric');82tree.insert('Tony');83tree.insert('Mick');84tree.insert('Ronnie');85tree.insert('Charlie');86tree.insert('Bill');87tree.insert('Derek');88tree.insert('Brian');89tree.insert('Phil');90tree.insert('Eric');91tree.insert('Tony
Using AI Code Generation
1var wptree = require('wptree');2var tree = new wptree.NameTree();3tree.add('a.b.c.d.e', 'value');4var wptree = require('wptree');5var tree = new wptree.NameTree();6tree.add('a.b.c.d.e', 'value');7var wptree = require('wptree');8var tree = new wptree.NameTree();9tree.add('a.b.c.d.e', 'value');10var wptree = require('wptree');11var tree = new wptree.NameTree();12tree.add('a.b.c.d.e', 'value');13var wptree = require('wptree');14var tree = new wptree.NameTree();15tree.add('a.b.c.d.e', 'value');16console.log(tree.get('
Using AI Code Generation
1var wptree = require('wptree')2var tree = new wptree.NameTree()3tree.add('name1', {name:'name1', value:1})4tree.add('name2', {name:'name2', value:2})5tree.add('name3', {name:'name3', value:3})6tree.add('name4', {name:'name4', value:4})7tree.add('name5', {name:'name5', value:5})8tree.add('name6', {name:'name6', value:6})9tree.add('name7', {name:'name7', value:7})10tree.add('name8', {name:'name8', value:8})11tree.add('name9', {name:'name9', value:9})12tree.add('name10', {name:'name10', value:10})13tree.add('name11', {name:'name11', value:11})14tree.add('name12', {name:'name12', value:12})15tree.add('name13', {name:'name13', value:13})16tree.add('name14', {name:'name14', value:14})17tree.add('name15', {name:'name15', value:15})18tree.add('name16', {name:'name16', value:16})19tree.add('name17', {name:'name17', value:17})20tree.add('name18', {name:'name18', value:18})21tree.add('name19', {name:'name19', value:19})22tree.add('name20', {name:'name20', value:20})23tree.add('name21', {name:'name21', value:21})24tree.add('name22', {name:'name22', value:22})25tree.add('name23', {name:'name23', value:23})26tree.add('name24', {name:'name24', value:24})27tree.add('name25', {name:'name25', value:25})28tree.add('name26', {name:'name26', value:26})29tree.add('name27', {name:'name27', value:27})30tree.add('name28', {name:'name28', value:28})31tree.add('name29', {name:'name29', value:29})32tree.add('name30', {name:'name30',
Using AI Code Generation
1var wptree = require('wptree');2var tree = new wptree.NameTree();3tree.add('John Smith');4tree.add('John Doe');5tree.add('Jane Smith');6var names = tree.get('John');7console.log(names);8var wptree = require('wptree');9var tree = new wptree.NameTree();10tree.add('John Smith');11tree.add('John Doe');12tree.add('Jane Smith');13var names = tree.get('John');14console.log(names);15var wptree = require('wptree');16var tree = new wptree.NameTree();17tree.add('John Smith');18tree.add('John Doe');19tree.add('Jane Smith');20var names = tree.get('John');21console.log(names);22var wptree = require('wptree');23var tree = new wptree.NameTree();24tree.add('John Smith');25tree.add('John Doe');26tree.add('Jane Smith');27var names = tree.get('John');28console.log(names);29var wptree = require('wptree');30var tree = new wptree.NameTree();31tree.add('John Smith');32tree.add('John Doe');33tree.add('Jane Smith');34var names = tree.get('John');35console.log(names);36var wptree = require('wptree');37var tree = new wptree.NameTree();38tree.add('John Smith');39tree.add('John Doe');40tree.add('Jane Smith');41var names = tree.get('John');42console.log(names);43var wptree = require('wptree');44var tree = new wptree.NameTree();45tree.add('John Smith');46tree.add('John Doe');47tree.add('Jane Smith');48var names = tree.get('John');49console.log(names);
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!