Loading...
More JavaScript Posts
const linearSearch = (arr, item) => {for (const i in arr) {if (arr[i] === item) return +i;}return -1;};linearSearch([2, 9, 9], 9); // 1linearSearch([2, 9, 9], 7); // -1
class TreeNode {constructor(data, depth) {this.data = data;this.children = [];this.depth = depth;}}function buildNaryTree(hosts) {const nodeMap = {};// Step 1: Create a map of nodes using fqdn as the keyhosts.forEach((host) => {nodeMap[host.fqdn] = new TreeNode(host, 0); // Initialize depth to 0});// Step 2: Iterate through the array and add each node to its parent's list of childrenhosts.forEach((host) => {if (host.displayParent) {if (nodeMap[host.displayParent]) {const parent = nodeMap[host.displayParent];const node = nodeMap[host.fqdn];node.depth = parent.depth + 1; // Update the depthparent.children.push(node);} else {console.error(`Parent with fqdn ${host.displayParent} not found for ${host.fqdn}`);}}});// Find the root nodes (nodes with no parent)const rootNodes = hosts.filter((host) => !host.displayParent).map((host) => nodeMap[host.fqdn]);return rootNodes;}function treeToObjects(node) {const result = [];function inOrder(node) {if (!node) {return;}// Visit the current node and add it to the resultresult.push(node.data);// Visit children nodes firstnode.children.forEach((child) => {inOrder(child);});}inOrder(node);return result;}// Usage exampleconst hosts = [{fqdn: 'fqdn_1',display_name: 'Host 1',},{fqdn: 'fqdn_2',display_name: 'Host 2',displayParent: 'fqdn_1',},{fqdn: 'fqdn_3',display_name: 'Host 3'},{fqdn: 'fqdn_4',display_name: 'Host 4',displayParent: 'fqdn_3',},{fqdn: 'fqdn_5',display_name: 'Host 5',displayParent: 'fqdn_1',},];const tree = buildNaryTree(hosts);// Function to convert the tree to an array of objectsfunction convertTreeToArray(nodes) {const result = [];nodes.forEach((node) => {result.push(...treeToObjects(node));});return result;}// Convert the tree back to an array of objectsconst arrayFromTree = convertTreeToArray(tree);console.log(arrayFromTree);
//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 quickSort = arr => {const a = [...arr];if (a.length < 2) return a;const pivotIndex = Math.floor(arr.length / 2);const pivot = a[pivotIndex];const [lo, hi] = a.reduce((acc, val, i) => {if (val < pivot || (val === pivot && i != pivotIndex)) {acc[0].push(val);} else if (val > pivot) {acc[1].push(val);}return acc;},[[], []]);return [...quickSort(lo), pivot, ...quickSort(hi)];};console.log(quickSort([1, 6, 1, 5, 3, 2, 1, 4])) // [1, 1, 1, 2, 3, 4, 5, 6]
const levenshteinDistance = (s, t) => {if (!s.length) return t.length;if (!t.length) return s.length;const arr = [];for (let i = 0; i <= t.length; i++) {arr[i] = [i];for (let j = 1; j <= s.length; j++) {arr[i][j] =i === 0? j: Math.min(arr[i - 1][j] + 1,arr[i][j - 1] + 1,arr[i - 1][j - 1] + (s[j - 1] === t[i - 1] ? 0 : 1));}}return arr[t.length][s.length];};levenshteinDistance('duck', 'dark'); // 2
const flat = arr => [].concat.apply([], arr.map(a => Array.isArray(a) ? flat(a) : a));// Orconst flat = arr => arr.reduce((a, b) => Array.isArray(b) ? [...a, ...flat(b)] : [...a, b], []);// Or// See the browser compatibility at https://caniuse.com/#feat=array-flatconst flat = arr => arr.flat();// Exampleflat(['cat', ['lion', 'tiger']]); // ['cat', 'lion', 'tiger']