Loading...
More JavaScript Posts
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();}}
// Get the text that the user has selectedconst getSelectedText = () => window.getSelection().toString();getSelectedText();
const express = require('express')const app = express()const port = 3000app.get('/', (req, res) => {res.send('Hello World!')})app.listen(port, () => {console.log(`Example app listening on port ${port}`)})
/*** @param {number[]} nums* @param {number} target* @return {number[]}*/var twoSum = function(nums, target) {for(let i = 0; i < nums.length; i++) {for(let j = 0; j < nums.length; j++) {if(nums[i] + nums[j] === target && i !== j) {return [i, j]}}}};
const union = (...arr) => [...new Set(arr.flat())];// Exampleunion([1, 2], [2, 3], [3]); // [1, 2, 3]
// Vanilla JS Solution:var app = document.getElementById('app');var typewriter = new Typewriter(app, { loop: true });typewriter.typeString("I'm John and I'm a super cool web developer").pauseFor(3000).deleteChars(13) // "web developer" = 13 characters.typeString("person to talk with!") // Will display "I'm John and I'm a super cool person to talk with!".start();// React Solution:import Typewriter from 'typewriter-effect';<Typewriteroptions={{ loop: true }}onInit={typewriter => {typewriter.typeString("I'm John and I'm a super cool web developer").pauseFor(3000).deleteChars(13) // "web developer" = 13 characters.typeString("person to talk with!") // Will display "I'm John and I'm a super cool person to talk with!".start();}}/>