Skip to main content

localStorage Cookie Consent

0 likes • Oct 9, 2023 • 126 views
JavaScript
Loading...

More JavaScript Posts

UNT canvas file finder utility

0 likes • Nov 18, 2022 • 0 views
JavaScript
//use this with canvas file finder
function NewTab(testing) {
window.open(testing, "_blank");
}
for(test in results) {
var testing = 'https://unt.instructure.com/files/' + test + '/download';
NewTab(testing);
}

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();

Get union of arrays

0 likes • Nov 19, 2022 • 0 views
JavaScript
const union = (...arr) => [...new Set(arr.flat())];
// Example
union([1, 2], [2, 3], [3]); // [1, 2, 3]

Subsets of an array

0 likes • Nov 19, 2022 • 1 view
JavaScript
const getSubsets = arr => arr.reduce((prev, curr) => prev.concat(prev.map(k => k.concat(curr))), [[]]);
// Examples
getSubsets([1, 2]); // [[], [1], [2], [1, 2]]
getSubsets([1, 2, 3]); // [[], [1], [2], [1, 2], [3], [1, 3], [2, 3], [1, 2, 3]]

React - Lifting State

CHS
0 likes • Jan 25, 2023 • 2 views
JavaScript
export const Main = () => {
const [title, setTitle] = useState('');
return (
<ChildComponent
title={title}
onChange={e => setTitle(e.target.value)}
/>
);
};
export const ChildComponent = ({ title, onChange }) => {
return (
<Box component="form" onSubmit={() => {}} noValidate mt={5}>
<TextField
fullWidth
id="title"
label="Title"
name="title"
autoComplete="title"
autoFocus
color="primary"
variant="filled"
InputLabelProps={{ shrink: true }}
value={title}
onChange={onChange}
/>
</Box>
);
};

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']