Skip to main content

test

0 likes • Sep 10, 2023 • 3 views
JavaScript
Loading...

More JavaScript Posts

format duration

0 likes • Nov 19, 2022 • 0 views
JavaScript
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'

Get timezone as a string

0 likes • Nov 19, 2022 • 0 views
JavaScript
const getTimezone = () => Intl.DateTimeFormat().resolvedOptions().timeZone;
// Example
getTimezone();

bucket sort

0 likes • Nov 19, 2022 • 0 views
JavaScript
const bucketSort = (arr, size = 5) => {
const min = Math.min(...arr);
const max = Math.max(...arr);
const buckets = Array.from(
{ length: Math.floor((max - min) / size) + 1 },
() => []
);
arr.forEach(val => {
buckets[Math.floor((val - min) / size)].push(val);
});
return buckets.reduce((acc, b) => [...acc, ...b.sort((a, b) => a - b)], []);
};
bucketSort([6, 3, 4, 1]); // [1, 3, 4, 6]

Reddit Regex Comment Upvoter

0 likes • May 17, 2021 • 2 views
JavaScript
//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();

tree traversal

0 likes • Nov 19, 2022 • 1 view
JavaScript
class TreeNode {
constructor(key, value = key, parent = null) {
this.key = key;
this.value = value;
this.parent = parent;
this.children = [];
}
get isLeaf() {
return this.children.length === 0;
}
get hasChildren() {
return !this.isLeaf;
}
}
class Tree {
constructor(key, value = key) {
this.root = new TreeNode(key, value);
}
*preOrderTraversal(node = this.root) {
yield node;
if (node.children.length) {
for (let child of node.children) {
yield* this.preOrderTraversal(child);
}
}
}
*postOrderTraversal(node = this.root) {
if (node.children.length) {
for (let child of node.children) {
yield* this.postOrderTraversal(child);
}
}
yield node;
}
insert(parentNodeKey, key, value = key) {
for (let node of this.preOrderTraversal()) {
if (node.key === parentNodeKey) {
node.children.push(new TreeNode(key, value, node));
return true;
}
}
return false;
}
remove(key) {
for (let node of this.preOrderTraversal()) {
const filtered = node.children.filter(c => c.key !== key);
if (filtered.length !== node.children.length) {
node.children = filtered;
return true;
}
}
return false;
}
find(key) {
for (let node of this.preOrderTraversal()) {
if (node.key === key) return node;
}
return undefined;
}
}
const tree = new Tree(1, 'AB');
tree.insert(1, 11, 'AC');
tree.insert(1, 12, 'BC');
tree.insert(12, 121, 'BG');
[...tree.preOrderTraversal()].map(x => x.value);
// ['AB', 'AC', 'BC', 'BCG']
tree.root.value; // 'AB'
tree.root.hasChildren; // true
tree.find(12).isLeaf; // false
tree.find(121).isLeaf; // true
tree.find(121).parent.value; // 'BC'
tree.remove(12);
[...tree.postOrderTraversal()].map(x => x.value);
// ['AC', 'AB']

luhnCheck implementation

0 likes • Nov 19, 2022 • 0 views
JavaScript
const luhnCheck = num => {
let arr = (num + '')
.split('')
.reverse()
.map(x => parseInt(x));
let lastDigit = arr.splice(0, 1)[0];
let sum = arr.reduce(
(acc, val, i) => (i % 2 !== 0 ? acc + val : acc + ((val *= 2) > 9 ? val - 9 : val)),
0
);
sum += lastDigit;
return sum % 10 === 0;
};
luhnCheck('4485275742308327'); // true
luhnCheck(6011329933655299); // true
luhnCheck(123456789); // false