Skip to main content

unzipWith() function

0 likes • Nov 19, 2022 • 0 views
JavaScript
Loading...

More JavaScript Posts

greatest common denominator

0 likes • Nov 19, 2022 • 0 views
JavaScript
const gcd = (...arr) => {
const _gcd = (x, y) => (!y ? x : gcd(y, x % y));
return [...arr].reduce((a, b) => _gcd(a, b));
};
gcd(8, 36); // 4
gcd(...[12, 8, 32]); // 4

heap sort

0 likes • Nov 19, 2022 • 0 views
JavaScript
const heapsort = arr => {
const a = [...arr];
let l = a.length;
const heapify = (a, i) => {
const left = 2 * i + 1;
const right = 2 * i + 2;
let max = i;
if (left < l && a[left] > a[max]) max = left;
if (right < l && a[right] > a[max]) max = right;
if (max !== i) {
[a[max], a[i]] = [a[i], a[max]];
heapify(a, max);
}
};
for (let i = Math.floor(l / 2); i >= 0; i -= 1) heapify(a, i);
for (i = a.length - 1; i > 0; i--) {
[a[0], a[i]] = [a[i], a[0]];
l--;
heapify(a, 0);
}
return a;
};
heapsort([6, 3, 4, 1]); // [1, 3, 4, 6]

Zybooks?

0 likes • Oct 29, 2020 • 2 views
JavaScript
questions = Array.from(document.querySelectorAll(".multiple-choice-question"));
async function hackRadio(question){
if(question.querySelector(".question-chevron").getAttribute("aria-label") == "Question completed") return;
let answerChoices = question.querySelectorAll(".zb-radio-button");
async function guess(answerChoice){
answerChoice.querySelector("label").click();
}
let pause = 0;
for(let answerChoice of answerChoices){
setTimeout(()=>{guess(answerChoice)},pause); //No need to check given that it will record the correct answer anyways.
pause += 1000;
}
}
for(let question of questions){
hackRadio(question);
}
questions = Array.from(document.querySelectorAll(".short-answer-question"));
async function hackShortAnswer(question){
if(question.querySelector(".question-chevron").getAttribute("aria-label") == "Question completed") return;
question.querySelector("textarea").value = question.querySelector(".forfeit-answer").textContent;
//question.querySelector(".check-button").click(); They are smart bastards
}
for(let question of questions){
const showAnswerButton = question.querySelector(".show-answer-button");
showAnswerButton.click();
showAnswerButton.click();
hackShortAnswer(question);
}

React Short Circuit Evaluation

CHS
0 likes • Feb 3, 2021 • 0 views
JavaScript
import React, { useState } from 'react' import Welcome from '../components/Welcome' function About() { const [showWelcome, setShowWelcome] = useState(false) return ( <div> {showWelcome ? <Welcome /> : null} </div> <div> {showWelcome && <Welcome /> } </div> ) } export default App

linked list

0 likes • Nov 19, 2022 • 1 view
JavaScript
class LinkedList {
constructor() {
this.nodes = [];
}
get size() {
return this.nodes.length;
}
get head() {
return this.size ? this.nodes[0] : null;
}
get tail() {
return this.size ? this.nodes[this.size - 1] : null;
}
insertAt(index, value) {
const previousNode = this.nodes[index - 1] || null;
const nextNode = this.nodes[index] || null;
const node = { value, next: nextNode };
if (previousNode) previousNode.next = node;
this.nodes.splice(index, 0, node);
}
insertFirst(value) {
this.insertAt(0, value);
}
insertLast(value) {
this.insertAt(this.size, value);
}
getAt(index) {
return this.nodes[index];
}
removeAt(index) {
const previousNode = this.nodes[index - 1];
const nextNode = this.nodes[index + 1] || null;
if (previousNode) previousNode.next = nextNode;
return this.nodes.splice(index, 1);
}
clear() {
this.nodes = [];
}
reverse() {
this.nodes = this.nodes.reduce(
(acc, { value }) => [{ value, next: acc[0] || null }, ...acc],
[]
);
}
*[Symbol.iterator]() {
yield* this.nodes;
}
}
const list = new LinkedList();
list.insertFirst(1);
list.insertFirst(2);
list.insertFirst(3);
list.insertLast(4);
list.insertAt(3, 5);
list.size; // 5
list.head.value; // 3
list.head.next.value; // 2
list.tail.value; // 4
[...list.map(e => e.value)]; // [3, 2, 1, 5, 4]
list.removeAt(1); // 2
list.getAt(1).value; // 1
list.head.next.value; // 1
[...list.map(e => e.value)]; // [3, 1, 5, 4]
list.reverse();
[...list.map(e => e.value)]; // [4, 5, 1, 3]
list.clear();
list.size; // 0

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