• Nov 19, 2022 •CodeCatch
0 likes • 0 views
const formatDuration = ms => { if (ms < 0) ms = -ms; const time = { day: Math.floor(ms / 86400000), hour: Math.floor(ms / 3600000) % 24, minute: Math.floor(ms / 60000) % 60, second: Math.floor(ms / 1000) % 60, millisecond: Math.floor(ms) % 1000 }; return Object.entries(time) .filter(val => val[1] !== 0) .map(([key, val]) => `${val} ${key}${val !== 1 ? 's' : ''}`) .join(', '); }; formatDuration(1001); // '1 second, 1 millisecond' formatDuration(34325055574); // '397 days, 6 hours, 44 minutes, 15 seconds, 574 milliseconds'
• Sep 13, 2023 •C S
0 likes • 2 views
/** * @param {number[]} nums * @return {boolean} */ var containsDuplicate = function(nums) { return new Set(nums).size !== nums.length };
• Jan 26, 2023 •AustinLeath
0 likes • 5 views
function printHeap(heap, index, level) { if (index >= heap.length) { return; } console.log(" ".repeat(level) + heap[index]); printHeap(heap, 2 * index + 1, level + 1); printHeap(heap, 2 * index + 2, level + 1); } //You can call this function by passing in the heap array and the index of the root node, which is typically 0, and level = 0. let heap = [3, 8, 7, 15, 17, 30, 35, 2, 4, 5, 9]; printHeap(heap,0,0)
/** * @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] } } } };
• May 17, 2021 •LeifMessinger
//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 good let comments = document.getElementsByClassName("_3tw__eCCe7j-epNCKGXUKk"); //Comment class let commentsToUpvote = []; const regexToMatch = /wtf/gi; for(let comment of comments){ let text = comment.querySelector("._1qeIAgB0cPwnLhDF9XSiJM"); //Comment content class if(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 doing comment.querySelector(".icon-upvote").click(); } function slowRecurse(){ let comment = commentsToUpvote.pop(); //It's gonna go bottom to top but whatever if(comment != undefined){ upvote(comment); setTimeout(slowRecurse,500); } } slowRecurse();
• Oct 15, 2022 •CodeCatch
1 like • 280 views
const express = require('express') const app = express() const port = 3000 app.get('/', (req, res) => { res.send('Hello World!') }) app.listen(port, () => { console.log(`Example app listening on port ${port}`) })