Skip to main content

Redux Slice

Mar 11, 2021CHS
Loading...

More JavaScript Posts

insertion sort

Nov 19, 2022CodeCatch

0 likes • 0 views

const insertionSort = arr =>
arr.reduce((acc, x) => {
if (!acc.length) return [x];
acc.some((y, j) => {
if (x <= y) {
acc.splice(j, 0, x);
return true;
}
if (x > y && j === acc.length - 1) {
acc.splice(j + 1, 0, x);
return true;
}
return false;
});
return acc;
}, []);
insertionSort([6, 3, 4, 1]); // [1, 3, 4, 6]

arithmetic progression

Nov 19, 2022CodeCatch

0 likes • 0 views

const arithmeticProgression = (n, lim) =>
Array.from({ length: Math.ceil(lim / n) }, (_, i) => (i + 1) * n );
arithmeticProgression(5, 25); // [5, 10, 15, 20, 25]

geometric progression

Nov 19, 2022CodeCatch

0 likes • 0 views

const geometricProgression = (end, start = 1, step = 2) =>
Array.from({
length: Math.floor(Math.log(end / start) / Math.log(step)) + 1,
}).map((_, i) => start * step ** i);
geometricProgression(256); // [1, 2, 4, 8, 16, 32, 64, 128, 256]
geometricProgression(256, 3); // [3, 6, 12, 24, 48, 96, 192]
geometricProgression(256, 1, 4); // [1, 4, 16, 64, 256]

copy to clipboard

Nov 19, 2022CodeCatch

0 likes • 0 views

const copyToClipboard = str => {
const el = document.createElement('textarea');
el.value = str;
el.setAttribute('readonly', '');
el.style.position = 'absolute';
el.style.left = '-9999px';
document.body.appendChild(el);
const selected =
document.getSelection().rangeCount > 0
? document.getSelection().getRangeAt(0)
: false;
el.select();
document.execCommand('copy');
document.body.removeChild(el);
if (selected) {
document.getSelection().removeAllRanges();
document.getSelection().addRange(selected);
}
};
copyToClipboard('Lorem ipsum'); // 'Lorem ipsum' copied to clipboard.

power set

Nov 19, 2022CodeCatch

0 likes • 2 views

const powerset = arr =>
arr.reduce((a, v) => a.concat(a.map(r => r.concat(v))), [[]]);
powerset([1, 2]); // [[], [1], [2], [1, 2]]

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