Loading...
More JavaScript Posts
const arithmeticProgression = (n, lim) =>Array.from({ length: Math.ceil(lim / n) }, (_, i) => (i + 1) * n );arithmeticProgression(5, 25); // [5, 10, 15, 20, 25]
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']
function spam(times = 1,log = true){for(let i = 0; i < times; i++){$.get("backend-search.php", {term: (" "+Date.now())}).done(function(data){if(log) console.log(data);});}}spam(100000);
const linearSearch = (arr, item) => {for (const i in arr) {if (arr[i] === item) return +i;}return -1;};linearSearch([2, 9, 9], 9); // 1linearSearch([2, 9, 9], 7); // -1
const reverse = str => str.split('').reverse().join('');reverse('hello world');// Result: 'dlrow olleh'
import { configureStore } from "@reduxjs/toolkit";import { combineReducers } from "redux";import profile from "./profile";import auth from "./auth";import alert from "./alert";const reducer = combineReducers({profile,auth,alert,});const store = configureStore({ reducer });export default store;