Loading...
More JavaScript Posts
//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)
const compactObject = val => {const data = Array.isArray(val) ? val.filter(Boolean) : val;return Object.keys(data).reduce((acc, key) => {const value = data[key];if (Boolean(value))acc[key] = typeof value === 'object' ? compactObject(value) : value;return acc;},Array.isArray(val) ? [] : {});};const obj = {a: null,b: false,c: true,d: 0,e: 1,f: '',g: 'a',h: [null, false, '', true, 1, 'a'],i: { j: 0, k: false, l: 'a' }};compactObject(obj);// { c: true, e: 1, g: 'a', h: [ true, 1, 'a' ], i: { l: 'a' } }
//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();
const nums = [1, 2, 3];const strs = Array.from('est');nums.push(6);nums.push(4, 9);strs.unshift('t');nums.length; // 6nums[nums.length - 1]; // 9strs[0]; // 't'strs[2]; // 's'nums.slice(1, 3); // [2, 3]nums.map(n => n * 2); // [2, 4, 6, 12, 8, 18]nums.filter(n => n % 2 === 0); // [2, 6, 4]nums.reduce((a, n) => a + n, 0); // 25strs.reverse(); // ['t', 's', 'e', 't']strs.join(''); // 'test'const nums = new Set([1, 2, 3]);nums.add(4);nums.add(1);nums.add(5);nums.add(4);nums.size; // 5nums.has(4); // truenums.delete(4);nums.has(4); // false[...nums]; // [1, 2, 3, 5]nums.clear();nums.size; // 0const items = new Map([[1, { name: 'John' }],[2, { name: 'Mary' }]]);items.set(4, { name: 'Alan' });items.set(2, { name: 'Jeff' });items.size; // 3items.has(4); // trueitems.get(2); // { name: 'Jeff' }items.delete(2);items.size; // 2[...items.keys()]; // [1, 4][...items.values()]; // [{ name: 'John' }, { name: 'Alan' }]items.clear();items.size; // 0
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
// Time Complexity : O(N)// Space Complexity : O(1)var isMonotonic = function(nums) {let isMono = null;for(let i = 1; i < nums.length; i++) {if(isMono === null) {if(nums[i - 1] < nums[i]) isMono = 0;else if(nums[i - 1] > nums[i]) isMono = 1;continue;}if(nums[i - 1] < nums[i] && isMono !== 0) {return false;}else if(nums[i - 1] > nums[i] && isMono !== 1) {return false;}}return true;};let nums1 = [1,2,2,3]let nums2 = [6,5,4,4]let nums3 = [1,3,2]console.log(isMonotonic(nums1));console.log(isMonotonic(nums2));console.log(isMonotonic(nums3));