Loading...
More JavaScript Posts
const nums = [1, 2, 3];const strs = Array.from('est');nums.push(6);nums.push(4, 9);strs.unshift('t');nums.length; // 6nums[nums.length - 1]; // 9strs[0]; // 't'strs[2]; // 's'nums.slice(1, 3); // [2, 3]nums.map(n => n * 2); // [2, 4, 6, 12, 8, 18]nums.filter(n => n % 2 === 0); // [2, 6, 4]nums.reduce((a, n) => a + n, 0); // 25strs.reverse(); // ['t', 's', 'e', 't']strs.join(''); // 'test'const nums = new Set([1, 2, 3]);nums.add(4);nums.add(1);nums.add(5);nums.add(4);nums.size; // 5nums.has(4); // truenums.delete(4);nums.has(4); // false[...nums]; // [1, 2, 3, 5]nums.clear();nums.size; // 0const items = new Map([[1, { name: 'John' }],[2, { name: 'Mary' }]]);items.set(4, { name: 'Alan' });items.set(2, { name: 'Jeff' });items.size; // 3items.has(4); // trueitems.get(2); // { name: 'Jeff' }items.delete(2);items.size; // 2[...items.keys()]; // [1, 4][...items.values()]; // [{ name: 'John' }, { name: 'Alan' }]items.clear();items.size; // 0
const compactObject = val => {const data = Array.isArray(val) ? val.filter(Boolean) : val;return Object.keys(data).reduce((acc, key) => {const value = data[key];if (Boolean(value))acc[key] = typeof value === 'object' ? compactObject(value) : value;return acc;},Array.isArray(val) ? [] : {});};const obj = {a: null,b: false,c: true,d: 0,e: 1,f: '',g: 'a',h: [null, false, '', true, 1, 'a'],i: { j: 0, k: false, l: 'a' }};compactObject(obj);// { c: true, e: 1, g: 'a', h: [ true, 1, 'a' ], i: { l: 'a' } }
function printHeap(heap, index, level) {if (index >= heap.length) {return;}console.log(" ".repeat(level) + heap[index]);printHeap(heap, 2 * index + 1, level + 1);printHeap(heap, 2 * index + 2, level + 1);}//You can call this function by passing in the heap array and the index of the root node, which is typically 0, and level = 0.let heap = [3, 8, 7, 15, 17, 30, 35, 2, 4, 5, 9];printHeap(heap,0,0)
class SequentialQueue{ //if you want it to go backwards, too badnext(){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());}
//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 goodlet comments = document.getElementsByClassName("_3tw__eCCe7j-epNCKGXUKk"); //Comment classlet commentsToUpvote = [];const regexToMatch = /wtf/gi;for(let comment of comments){let text = comment.querySelector("._1qeIAgB0cPwnLhDF9XSiJM"); //Comment content classif(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 doingcomment.querySelector(".icon-upvote").click();}function slowRecurse(){let comment = commentsToUpvote.pop(); //It's gonna go bottom to top but whateverif(comment != undefined){upvote(comment);setTimeout(slowRecurse,500);}}slowRecurse();
router.post("/", async (req, res) => {const { email, password } = req.body;try {if (!email) return res.status(400).json({ msg: "An email is required" });if (!password)return res.status(400).json({ msg: "A password is required" });const user = await User.findOne({ email }).select("_id password");if (!user) return res.status(400).json({ msg: "Invalid credentials" });const match = await bcrypt.compare(password, user.password);if (!match) return res.status(400).json({ msg: "Invalid credentials" });const accessToken = genAccessToken({ id: user._id });const refreshToken = genRefreshToken({ id: user._id });res.cookie("token", refreshToken, {expires: new Date(Date.now() + 604800),httpOnly: true,});res.json({ accessToken });} catch (err) {console.log(err.message);res.status(500).json({ msg: "Error logging in user" });}});