Loading...
More JavaScript Posts
class Graph {constructor(directed = true) {this.directed = directed;this.nodes = [];this.edges = new Map();}addNode(key, value = key) {this.nodes.push({ key, value });}addEdge(a, b, weight) {this.edges.set(JSON.stringify([a, b]), { a, b, weight });if (!this.directed)this.edges.set(JSON.stringify([b, a]), { a: b, b: a, weight });}removeNode(key) {this.nodes = this.nodes.filter(n => n.key !== key);[...this.edges.values()].forEach(({ a, b }) => {if (a === key || b === key) this.edges.delete(JSON.stringify([a, b]));});}removeEdge(a, b) {this.edges.delete(JSON.stringify([a, b]));if (!this.directed) this.edges.delete(JSON.stringify([b, a]));}findNode(key) {return this.nodes.find(x => x.key === key);}hasEdge(a, b) {return this.edges.has(JSON.stringify([a, b]));}setEdgeWeight(a, b, weight) {this.edges.set(JSON.stringify([a, b]), { a, b, weight });if (!this.directed)this.edges.set(JSON.stringify([b, a]), { a: b, b: a, weight });}getEdgeWeight(a, b) {return this.edges.get(JSON.stringify([a, b])).weight;}adjacent(key) {return [...this.edges.values()].reduce((acc, { a, b }) => {if (a === key) acc.push(b);return acc;}, []);}indegree(key) {return [...this.edges.values()].reduce((acc, { a, b }) => {if (b === key) acc++;return acc;}, 0);}outdegree(key) {return [...this.edges.values()].reduce((acc, { a, b }) => {if (a === key) acc++;return acc;}, 0);}}const g = new Graph();g.addNode('a');g.addNode('b');g.addNode('c');g.addNode('d');g.addEdge('a', 'c');g.addEdge('b', 'c');g.addEdge('c', 'b');g.addEdge('d', 'a');g.nodes.map(x => x.value); // ['a', 'b', 'c', 'd'][...g.edges.values()].map(({ a, b }) => `${a} => ${b}`);// ['a => c', 'b => c', 'c => b', 'd => a']g.adjacent('c'); // ['b']g.indegree('c'); // 2g.outdegree('c'); // 1g.hasEdge('d', 'a'); // trueg.hasEdge('a', 'd'); // falseg.removeEdge('c', 'b');[...g.edges.values()].map(({ a, b }) => `${a} => ${b}`);// ['a => c', 'b => c', 'd => a']g.removeNode('c');g.nodes.map(x => x.value); // ['a', 'b', 'd'][...g.edges.values()].map(({ a, b }) => `${a} => ${b}`);// ['d => a']g.setEdgeWeight('d', 'a', 5);g.getEdgeWeight('d', 'a'); // 5
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
//Upvotes comments according to a regex pattern. I tried it out on https://www.reddit.com/r/VALORANT/comments/ne74n4/please_admire_this_clip_precise_gunplay_btw///Known bugs://If a comment was already upvoted, it gets downvoted//For some reason, it has around a 50-70% success rate. The case insensitive bit works, but I think some of the query selector stuff gets changed. Still, 50% is goodlet comments = document.getElementsByClassName("_3tw__eCCe7j-epNCKGXUKk"); //Comment classlet commentsToUpvote = [];const regexToMatch = /wtf/gi;for(let comment of comments){let text = comment.querySelector("._1qeIAgB0cPwnLhDF9XSiJM"); //Comment content classif(text == undefined){continue;}text = text.textContent;if(regexToMatch.test(text)){console.log(text);commentsToUpvote.push(comment);}}function upvote(comment){console.log(comment.querySelector(".icon-upvote")); //Just showing you what it's doingcomment.querySelector(".icon-upvote").click();}function slowRecurse(){let comment = commentsToUpvote.pop(); //It's gonna go bottom to top but whateverif(comment != undefined){upvote(comment);setTimeout(slowRecurse,500);}}slowRecurse();
//use this with canvas file finderfunction NewTab(testing) {window.open(testing, "_blank");}for(test in results) {var testing = 'https://unt.instructure.com/files/' + test + '/download';NewTab(testing);}
const reverse = str => str.split('').reverse().join('');reverse('hello world');// Result: 'dlrow olleh'
//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)