Loading...
More JavaScript Posts
const celsiusToFahrenheit = (celsius) => celsius * 9/5 + 32;const fahrenheitToCelsius = (fahrenheit) => (fahrenheit - 32) * 5/9;// ExamplescelsiusToFahrenheit(15); // 59celsiusToFahrenheit(0); // 32celsiusToFahrenheit(-20); // -4fahrenheitToCelsius(59); // 15fahrenheitToCelsius(32); // 0
const jwt = require("jsonwebtoken");const authToken = (req, res, next) => {const token = req.headers["x-auth-token"];try {req.user = jwt.verify(token, process.env.ACCESS_TOKEN_SECRET);next();} catch (err) {console.log(err.message);res.status(401).json({ msg: "Error authenticating token" });}};module.exports = authToken;
const binarySearch = (arr, item) => {let l = 0,r = arr.length - 1;while (l <= r) {const mid = Math.floor((l + r) / 2);const guess = arr[mid];if (guess === item) return mid;if (guess > item) r = mid - 1;else l = mid + 1;}return -1;};binarySearch([1, 2, 3, 4, 5], 1); // 0binarySearch([1, 2, 3, 4, 5], 5); // 4binarySearch([1, 2, 3, 4, 5], 6); // -1
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);}
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]
const deepMerge = (a, b, fn) =>[...new Set([...Object.keys(a), ...Object.keys(b)])].reduce((acc, key) => ({ ...acc, [key]: fn(key, a[key], b[key]) }),{});deepMerge({ a: true, b: { c: [1, 2, 3] } },{ a: false, b: { d: [1, 2, 3] } },(key, a, b) => (key === 'a' ? a && b : Object.assign({}, a, b)));// { a: false, b: { c: [ 1, 2, 3 ], d: [ 1, 2, 3 ] } }