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();}}
const unzipWith = (arr, fn) =>arr.reduce((acc, val) => (val.forEach((v, i) => acc[i].push(v)), acc),Array.from({length: Math.max(...arr.map(x => x.length))}).map(x => [])).map(val => fn(...val));unzipWith([[1, 10, 100],[2, 20, 200],],(...args) => args.reduce((acc, v) => acc + v, 0));// [3, 30, 300]
var d = new Date();var d = new Date(milliseconds);var d = new Date(dateString);var d = new Date(year, month, day, hours, minutes, seconds, milliseconds);
//Retries maxRetries number of times, meaning that if you have 0, then it'll run the function once.async function retryPromise(promiseFn, maxRetries = 3, delayOffset = 0, delayRandomRange = 0) {return new Promise((resolve, reject) => {promiseFn().then(resolve, (error) => { //On errorif (maxRetries <= 0) {return reject(error);} else {if(delayRandomRange * Math.random() + delayOffset < 1.0){return retryPromise(promiseFn, maxRetries - 1, delayOffset, delayRandomRange).then(resolve, reject);}else{return new Promise((resolveTwo, rejectTwo) => {setTimeout(() => {return retryPromise(promiseFn, maxRetries - 1, delayOffset, delayRandomRange).then(resolveTwo, rejectTwo);}, delayRandomRange * Math.random() + delayOffset);}).then(resolve, reject);}}});});}//Tests for that function.//Returns a function that will fail numTimes times, then will return aValuefunction functionThatReturnsAFunctionThatFailsACoupleTimes(numTimes, aValue){return async function(){if(numTimes == 0){return aValue;}numTimes--;throw false;};}const SMALL_NUMBER = 10;function testTest(failNumberOfTimes){let functionThatFailsACoupleTimes = functionThatReturnsAFunctionThatFailsACoupleTimes(failNumberOfTimes, true);//Test my testfor(let i = 0; i < (failNumberOfTimes * 2); ++i){function evalTest(val){let testTestFailedReason = "";if(val === undefined){testTestFailedReason = "Test test returned undefined for some reason.";}if((i + 1) > failNumberOfTimes){if(val === true){//We're good}else{testTestFailedReason = "Test test didn't return true when it should have.";}}else{if(val === false){//We're good}else{testTestFailedReason = "Test test didn't return false when it should have.";}}testTestFailedReason = testTestFailedReason || "Test test passed test case";console.log(testTestFailedReason, "at index", i, "where the function returned", val);}functionThatFailsACoupleTimes().then(evalTest, evalTest)};}testTest(SMALL_NUMBER);let testCaseCounter = 1;const throwsNoError = [(val)=>{if(val == true){console.log("Passed test case " + (testCaseCounter++));}else{console.error("Unexpected return value", val);}},()=>{console.error("It wasn't supposed to fail!")}]const throwsError = [(val)=>{console.error("It wasn't supposed to succeed!", val);},(val)=>{if(val == false){console.log("Passed test case " + (testCaseCounter++));}else{console.error("Unexpected return value", val);}}];//Runs SMALL_NUMBER times, because SMALL_NUMBER - 1 is the number of retries after the first onelet functionThatFailsACoupleTimes = functionThatReturnsAFunctionThatFailsACoupleTimes(SMALL_NUMBER - 1, true);retryPromise(functionThatFailsACoupleTimes, SMALL_NUMBER - 1).then(...throwsNoError)functionThatFailsACoupleTimes = functionThatReturnsAFunctionThatFailsACoupleTimes(SMALL_NUMBER - 1, true);//Runs SMALL_NUMBER - 1 times, because SMALL_NUMBER - 2 is the number of retries after the first oneretryPromise(functionThatFailsACoupleTimes, SMALL_NUMBER - 2).then(...throwsError)//Testing the delay. You'll have to wait a bit too.functionThatFailsACoupleTimes = functionThatReturnsAFunctionThatFailsACoupleTimes(SMALL_NUMBER - 1, true);//Runs SMALL_NUMBER times, because SMALL_NUMBER - 1 is the number of retries after the first oneretryPromise(functionThatFailsACoupleTimes, SMALL_NUMBER - 1, 500, 500).then(...throwsNoError)functionThatFailsACoupleTimes = functionThatReturnsAFunctionThatFailsACoupleTimes(SMALL_NUMBER - 1, true);//Runs SMALL_NUMBER - 1 times, because SMALL_NUMBER - 2 is the number of retries after the first oneretryPromise(functionThatFailsACoupleTimes, SMALL_NUMBER - 2, 500, 500).then(...throwsError)
alert("bruh")
class LinkedList {constructor() {this.nodes = [];}get size() {return this.nodes.length;}get head() {return this.size ? this.nodes[0] : null;}get tail() {return this.size ? this.nodes[this.size - 1] : null;}insertAt(index, value) {const previousNode = this.nodes[index - 1] || null;const nextNode = this.nodes[index] || null;const node = { value, next: nextNode };if (previousNode) previousNode.next = node;this.nodes.splice(index, 0, node);}insertFirst(value) {this.insertAt(0, value);}insertLast(value) {this.insertAt(this.size, value);}getAt(index) {return this.nodes[index];}removeAt(index) {const previousNode = this.nodes[index - 1];const nextNode = this.nodes[index + 1] || null;if (previousNode) previousNode.next = nextNode;return this.nodes.splice(index, 1);}clear() {this.nodes = [];}reverse() {this.nodes = this.nodes.reduce((acc, { value }) => [{ value, next: acc[0] || null }, ...acc],[]);}*[Symbol.iterator]() {yield* this.nodes;}}const list = new LinkedList();list.insertFirst(1);list.insertFirst(2);list.insertFirst(3);list.insertLast(4);list.insertAt(3, 5);list.size; // 5list.head.value; // 3list.head.next.value; // 2list.tail.value; // 4[...list.map(e => e.value)]; // [3, 2, 1, 5, 4]list.removeAt(1); // 2list.getAt(1).value; // 1list.head.next.value; // 1[...list.map(e => e.value)]; // [3, 1, 5, 4]list.reverse();[...list.map(e => e.value)]; // [4, 5, 1, 3]list.clear();list.size; // 0