Loading...
More JavaScript Posts
import React, { useState } from 'react' import Welcome from '../components/Welcome' function About() { const [showWelcome, setShowWelcome] = useState(false) return ( <div> {showWelcome ? <Welcome /> : null} </div> <div> {showWelcome && <Welcome /> } </div> ) } export default App
class Timer{start(){this.startTime = Date.now();}stop(){const dt = Date.now() - this.startTime;console.log(dt);return dt;}getExecTime(fun, asynchronous){return asynchronous ? this.getAsyncExecTime(fun) : this.getSyncExecTime(fun);}getSyncExecTime(fun){this.start();fun();return this.stop();}async getAsyncExecTime(fun){this.start();await fun();return this.stop();}static average(fun, numTimes, asynchronous = false){const times = [];const promises = [];let timer = new Timer();if(asynchronous){for(let i = 0; i < numTimes; i++){timer.getAsyncExecTime(fun);}}else{for(let i = 0; i < numTimes; i++){timer.getSyncExecTime(fun);}}return (times => times.reduce(((sum, acc) => sume + acc), 0) / times.length);}static async timeAsync(fun, numTimes){if(numTimes <= 1) return new Timer().getAsyncExecTime(fun);const promises = [];let timer = new Timer(true);for(let i = 0; i < numTimes; i++){promises.push(fun().catch((e)=>{}));}try{await Promise.all(promises).catch((e)=>{});}catch(e){//do absolutely nothing}return timer.stop();}constructor(start = false){if(start) this.start();}}
const reverse = str => str.split('').reverse().join('');reverse('hello world');// Result: 'dlrow olleh'
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']
//JavaScript program to swap two variables//take input from the userslet a = prompt('Enter the first variable: ');let b = prompt('Enter the second variable: ');// XOR operatora = a ^ bb = a ^ ba = a ^ bconsole.log(`The value of a after swapping: ${a}`);console.log(`The value of b after swapping: ${b}`);
const toFixed = (n, fixed) => ~~(Math.pow(10, fixed) * n) / Math.pow(10, fixed);// ExamplestoFixed(25.198726354, 1); // 25.1toFixed(25.198726354, 2); // 25.19toFixed(25.198726354, 3); // 25.198toFixed(25.198726354, 4); // 25.1987toFixed(25.198726354, 5); // 25.19872toFixed(25.198726354, 6); // 25.198726