• Nov 19, 2022 •CodeCatch
0 likes • 2 views
const arithmeticProgression = (n, lim) => Array.from({ length: Math.ceil(lim / n) }, (_, i) => (i + 1) * n ); arithmeticProgression(5, 25); // [5, 10, 15, 20, 25]
• Oct 15, 2023 •LeifMessinger
0 likes • 7 views
//Disclaimer: I do not condone mass web scraping of ESPN websites, and this is just theoretical code that hasn't been used. //Scrape player stats off of https://www.espn.com/nfl/team/roster/_/name/phi/philadelphia-eagles //Example player JS object /* { "shortName": "S. Opeta", "name": "Sua Opeta", "href": "http://www.espn.com/nfl/player/_/id/3121009/sua-opeta", "uid": "s:20~l:28~a:3121009", "guid": "dec19157-e984-f981-3724-498281328c97", "id": "3121009", "height": "6' 4\"", "weight": "305 lbs", "age": 27, "position": "G", "jersey": "78", "birthDate": "08/15/96", "headshot": "https://a.espncdn.com/i/headshots/nfl/players/full/3121009.png", "lastName": "Sua Opeta", "experience": 4, "college": "Weber State" } */ //The page needs to be focused, so you have to put this code in a bookmarklet and click the page before clicking it. Allegedly. navigator.clipboard.writeText(JSON.stringify( window["__espnfitt__"].page.content.roster.groups.flatMap((group)=>{ return group.athletes.map((athlete)=>{ //We can assume all football players are above 100 lbs and below 1000 lbs. athlete.weight = parseInt(athlete.weight.substring(0,3)); if(athlete.experience == "R") athlete.experience = 0; else athlete.experience = parseInt(athlete.experience); //We can assume players are at least 1 foot, or under 10 feet tall. athlete.inches = 12 * parseInt(athlete.height.substring(0, 1)); //Add feet in inches athlete.inches += parseInt(athlete.height.substring(2).replaceAll("\"", "").trim()); //Add remaining inches const monthDayYear = athlete.birthDate.split("/"); athlete.birthMonth = parseInt(monthDayYear[0]); athlete.birthDay = parseInt(monthDayYear[1]); athlete.birthYear = parseInt(monthDayYear[2]); //The only really useful stuff we get from this is Height, weight, left handedness, age, position, and birthday return athlete; }); }) , null, "\t")).then(null, ()=>{alert("That failed.")}); //Changes all the team links on this page https://www.espn.com/nfl/stats/team to the team's roster for easier scraping $$("table > tbody > tr > td > div > div > a").forEach((elm)=>{ elm.setAttribute("href", elm.getAttribute("href").replace("team/", "team/roster/")); });
0 likes • 0 views
const primes = num => { let arr = Array.from({ length: num - 1 }).map((x, i) => i + 2), sqroot = Math.floor(Math.sqrt(num)), numsTillSqroot = Array.from({ length: sqroot - 1 }).map((x, i) => i + 2); numsTillSqroot.forEach(x => (arr = arr.filter(y => y % x !== 0 || y === x))); return arr; }; primes(10); // [2, 3, 5, 7]
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
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 powerset = arr => arr.reduce((a, v) => a.concat(a.map(r => r.concat(v))), [[]]); powerset([1, 2]); // [[], [1], [2], [1, 2]]