Skip to main content

copy to clipboard

Nov 19, 2022CodeCatch
Loading...

More JavaScript Posts

CodeCatchCrasher

Feb 11, 2021LeifMessinger

0 likes • 2 views

function spam(times = 1,log = true){
for(let i = 0; i < times; i++){
$.get("backend-search.php", {term: (" "+Date.now())}).done(function(data){
if(log) console.log(data);
});
}
}
spam(100000);

LeetCode Product Except Self

Sep 13, 2023CHS

0 likes • 6 views

/**
* @param {number[]} nums
* @return {number[]}
*/
var productExceptSelf = function(nums) {
var output = [];
var leftMult = 1;
var rightMult = 1;
for (var i=nums.length - 1; i >= 0; i--) {
output[i] = rightMult;
rightMult *= nums[i];
console.log({output: JSON.stringify(output), i})
}
for (var j=0; j < nums.length; j++) {
output[j] *= leftMult;
leftMult *= nums[j];
console.log({output: JSON.stringify(output), j})
}
return output;
};
console.log(productExceptSelf([1, 2, 3, 4]))

Zybooks?

Oct 29, 2020LeifMessinger

0 likes • 2 views

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

doubly linked list

Nov 19, 2022CodeCatch

1 like • 0 views

class DoublyLinkedList {
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, previous: previousNode };
if (previousNode) previousNode.next = node;
if (nextNode) nextNode.previous = 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] || null;
const nextNode = this.nodes[index + 1] || null;
if (previousNode) previousNode.next = nextNode;
if (nextNode) nextNode.previous = previousNode;
return this.nodes.splice(index, 1);
}
clear() {
this.nodes = [];
}
reverse() {
this.nodes = this.nodes.reduce((acc, { value }) => {
const nextNode = acc[0] || null;
const node = { value, next: nextNode, previous: null };
if (nextNode) nextNode.previous = node;
return [node, ...acc];
}, []);
}
*[Symbol.iterator]() {
yield* this.nodes;
}
}
const list = new DoublyLinkedList();
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.tail.previous.value; // 5
[...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

Sequential Queue

Feb 6, 2021LeifMessinger

0 likes • 2 views

class SequentialQueue{ //if you want it to go backwards, too bad
next(){
return this.i++;
}
constructor(start = 0){
this.i = start;
}
}
const que = new SequentialQueue(0);
for(let i = 0; i < 10; i++){
console.log(que.next());
}

Redux Slice

Mar 11, 2021CHS

1 like • 0 views

import { createSlice } from "@reduxjs/toolkit";
const alert = createSlice({
name: "alert",
initialState: {
msg: "",
status: "",
},
reducers: {
set_alert: (state, action) => {
return {
...state,
msg: action.payload.msg,
status: action.payload.status,
};
},
clear_alert: (state, action) => {
return {
...state,
msg: "",
status: "",
};
},
},
});
export default alert.reducer;
const { set_alert, clear_alert } = alert.actions;
export const setAlert = (msg, status) => dispatch => {
dispatch(set_alert({ msg, status }));
setTimeout(() => dispatch(clear_alert()), 4000);
};
export const clearAlert = () => dispatch => {
dispatch(clear_alert());
};