Skip to main content

doubly linked list

Nov 19, 2022CodeCatch
Loading...

More JavaScript Posts

greatest common denominator

Nov 19, 2022CodeCatch

0 likes • 0 views

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

permutations

Nov 19, 2022CodeCatch

0 likes • 0 views

const permutations = arr => {
if (arr.length <= 2) return arr.length === 2 ? [arr, [arr[1], arr[0]]] : arr;
return arr.reduce(
(acc, item, i) =>
acc.concat(
permutations([...arr.slice(0, i), ...arr.slice(i + 1)]).map(val => [
item,
...val,
])
),
[]
);
};
permutations([1, 33, 5]);
// [ [1, 33, 5], [1, 5, 33], [33, 1, 5], [33, 5, 1], [5, 1, 33], [5, 33, 1] ]

Mee6 Spam Calculator

Mar 10, 2022LeifMessinger

0 likes • 1 view

//Use at https://mee6.xyz/leaderboard/732262089447702588
//Higher the spammy stat, the more spammy that person is.
//This is because mee doesn't give experience to people who post more comments in a minute.
function statBlock(title, value){
let elm = document.createElement("div");
elm.className = "leaderboardPlayerStatBlock";
let titleElm = document.createElement("div");
titleElm.className = "leaderboardPlayerStatName";
titleElm.textContent = title;
let valueElm = document.createElement("div");
valueElm.className = "leaderboardPlayerStatValue";
valueElm.textContent = value;
elm.appendChild(titleElm);
elm.appendChild(valueElm);
elm.remove = function(){
this.parentElement.removeChild(this);
}
return elm;
}
for(let player of Array.from(document.getElementsByClassName("leaderboardPlayer"))){
if(player.spamminess) player.spamminess.remove();
let messages = null;
let experience = null;
const statBlockArray = Array.from(player.querySelectorAll(".leaderboardPlayerStatBlock"));
for(let statBlock of statBlockArray){
const statName = statBlock.querySelector(".leaderboardPlayerStatName").textContent;
const text = statBlock.querySelector(".leaderboardPlayerStatValue").textContent;
const number = ((text.includes("k"))? (text.replace("k","") * 1000.0) : +(text));
if(statName.includes("MESSAGES")){
messages = number;
}else if(statName.includes("EXPERIENCE")){
experience = number;
}
}
const stats = player.querySelector(".leaderboardPlayerStats");
const messagesElement = stats.firstChild;
const spamminess = ((messages/experience)*2000.0).toFixed(2);
player.spamminess = stats.insertBefore(statBlock("SPAMMINESS", spamminess), messagesElement);
}

copyString.js

Nov 8, 2023LeifMessinger

0 likes • 6 views

function copyString(text){
async function navigatorClipboardCopy(text){
if(document.location.protocol != "https:"){
return false;
}
return new Promise((resolve, reject)=>{
navigator.clipboard.writeText(text).then(()=>{resolve(true);}, ()=>{resolve(false);});
});
}
function domCopy(text){
if(!(document.execCommand)){
console.warn("They finally deprecated document.execCommand!");
}
const input = document.createElement('textarea');
input.value = text;
document.body.appendChild(input);
input.select();
const success = document.execCommand('copy');
document.body.removeChild(input);
return success;
}
function promptCopy(){
prompt("Copy failed. Might have ... somewhere. Check console.", text);
console.log(text);
}
function done(){
alert("Copied to clipboard");
}
navigatorClipboardCopy(text).catch(()=>{return false;}).then((success)=>{
if(success){
done();
}else{
if(domCopy(text)){
done();
}else{
promptCopy();
}
}
});
}

Simple Express Server

Oct 15, 2022CodeCatch

1 like • 187 views

const express = require('express')
const app = express()
const port = 3000
app.get('/', (req, res) => {
res.send('Hello World!')
})
app.listen(port, () => {
console.log(`Example app listening on port ${port}`)
})

Bitwise XOR operator

Nov 19, 2022CodeCatch

0 likes • 1 view

//JavaScript program to swap two variables
//take input from the users
let a = prompt('Enter the first variable: ');
let b = prompt('Enter the second variable: ');
// XOR operator
a = a ^ b
b = a ^ b
a = a ^ b
console.log(`The value of a after swapping: ${a}`);
console.log(`The value of b after swapping: ${b}`);