Loading...
More JavaScript Posts
const union = (...arr) => [...new Set(arr.flat())];// Exampleunion([1, 2], [2, 3], [3]); // [1, 2, 3]
class TreeNode {constructor(key, value = key, parent = null) {this.key = key;this.value = value;this.parent = parent;this.children = [];}get isLeaf() {return this.children.length === 0;}get hasChildren() {return !this.isLeaf;}}class Tree {constructor(key, value = key) {this.root = new TreeNode(key, value);}*preOrderTraversal(node = this.root) {yield node;if (node.children.length) {for (let child of node.children) {yield* this.preOrderTraversal(child);}}}*postOrderTraversal(node = this.root) {if (node.children.length) {for (let child of node.children) {yield* this.postOrderTraversal(child);}}yield node;}insert(parentNodeKey, key, value = key) {for (let node of this.preOrderTraversal()) {if (node.key === parentNodeKey) {node.children.push(new TreeNode(key, value, node));return true;}}return false;}remove(key) {for (let node of this.preOrderTraversal()) {const filtered = node.children.filter(c => c.key !== key);if (filtered.length !== node.children.length) {node.children = filtered;return true;}}return false;}find(key) {for (let node of this.preOrderTraversal()) {if (node.key === key) return node;}return undefined;}}const tree = new Tree(1, 'AB');tree.insert(1, 11, 'AC');tree.insert(1, 12, 'BC');tree.insert(12, 121, 'BG');[...tree.preOrderTraversal()].map(x => x.value);// ['AB', 'AC', 'BC', 'BCG']tree.root.value; // 'AB'tree.root.hasChildren; // truetree.find(12).isLeaf; // falsetree.find(121).isLeaf; // truetree.find(121).parent.value; // 'BC'tree.remove(12);[...tree.postOrderTraversal()].map(x => x.value);// ['AC', 'AB']
// Time Complexity : O(N)// Space Complexity : O(1)var isMonotonic = function(nums) {let isMono = null;for(let i = 1; i < nums.length; i++) {if(isMono === null) {if(nums[i - 1] < nums[i]) isMono = 0;else if(nums[i - 1] > nums[i]) isMono = 1;continue;}if(nums[i - 1] < nums[i] && isMono !== 0) {return false;}else if(nums[i - 1] > nums[i] && isMono !== 1) {return false;}}return true;};let nums1 = [1,2,2,3]let nums2 = [6,5,4,4]let nums3 = [1,3,2]console.log(isMonotonic(nums1));console.log(isMonotonic(nums2));console.log(isMonotonic(nums3));
arr = [] // empty arrayconst isEmpty = arr => !Array.isArray(arr) || arr.length === 0;// ExamplesisEmpty([]); // trueisEmpty([1, 2, 3]); // false
alert("bruh")
const nums = [1, 2, 3];const strs = Array.from('est');nums.push(6);nums.push(4, 9);strs.unshift('t');nums.length; // 6nums[nums.length - 1]; // 9strs[0]; // 't'strs[2]; // 's'nums.slice(1, 3); // [2, 3]nums.map(n => n * 2); // [2, 4, 6, 12, 8, 18]nums.filter(n => n % 2 === 0); // [2, 6, 4]nums.reduce((a, n) => a + n, 0); // 25strs.reverse(); // ['t', 's', 'e', 't']strs.join(''); // 'test'const nums = new Set([1, 2, 3]);nums.add(4);nums.add(1);nums.add(5);nums.add(4);nums.size; // 5nums.has(4); // truenums.delete(4);nums.has(4); // false[...nums]; // [1, 2, 3, 5]nums.clear();nums.size; // 0const items = new Map([[1, { name: 'John' }],[2, { name: 'Mary' }]]);items.set(4, { name: 'Alan' });items.set(2, { name: 'Jeff' });items.size; // 3items.has(4); // trueitems.get(2); // { name: 'Jeff' }items.delete(2);items.size; // 2[...items.keys()]; // [1, 4][...items.values()]; // [{ name: 'John' }, { name: 'Alan' }]items.clear();items.size; // 0