• Nov 19, 2022 •CodeCatch
0 likes • 0 views
const hammingDistance = (num1, num2) => ((num1 ^ num2).toString(2).match(/1/g) || '').length; hammingDistance(2, 3); // 1
0 likes • 1 view
const head = arr => (arr && arr.length ? arr[0] : undefined); head([1, 2, 3]); // 1 head([]); // undefined head(null); // undefined head(undefined); // undefined
• Mar 10, 2022 •LeifMessinger
//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); }
0 likes • 3 views
const euclideanDistance = (a, b) => Math.hypot(...Object.keys(a).map(k => b[k] - a[k])); euclideanDistance([1, 1], [2, 3]); // ~2.2361 euclideanDistance([1, 1, 1], [2, 3, 2]); // ~2.4495
• Feb 6, 2021 •LeifMessinger
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()); }
• Mar 22, 2022 •LeifMessinger
//QM Helper by Leif Messinger //Groups numbers by number of bits and shows their binary representations. //To be used on x.com const minterms = prompt("Enter your minterms separated by commas").split(",").map(x => parseInt(x.trim())); const maxNumBits = minterms.reduce(function(a, b) { return Math.max(a, Math.log2(b)); }); const bitGroups = []; for(let i = 0; i < maxNumBits; ++i){ bitGroups.push([]); } for(const minterm of minterms){ let outputString = (minterm+" "); //Count the bits let count = 0; for (var i = maxNumBits; i >= 0; --i) { if((minterm >> i) & 1){ ++count; outputString += "1"; }else{ outputString += "0"; } } bitGroups[count].push(outputString); } document.body.textContent = ""; document.body.style.setProperty("white-space", "pre"); for(const group of bitGroups){ for(const outputString of group){ document.body.textContent += outputString + "\r\n"; } }