Skip to main content

Ticking clock

0 likes • Nov 19, 2022 • 0 views
JavaScript
Loading...

More JavaScript Posts

ESPN Web Scrape

0 likes • Oct 15, 2023 • 6 views
JavaScript
//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/"));
});

format duration

0 likes • Nov 19, 2022 • 0 views
JavaScript
const formatDuration = ms => {
if (ms < 0) ms = -ms;
const time = {
day: Math.floor(ms / 86400000),
hour: Math.floor(ms / 3600000) % 24,
minute: Math.floor(ms / 60000) % 60,
second: Math.floor(ms / 1000) % 60,
millisecond: Math.floor(ms) % 1000
};
return Object.entries(time)
.filter(val => val[1] !== 0)
.map(([key, val]) => `${val} ${key}${val !== 1 ? 's' : ''}`)
.join(', ');
};
formatDuration(1001); // '1 second, 1 millisecond'
formatDuration(34325055574); // '397 days, 6 hours, 44 minutes, 15 seconds, 574 milliseconds'

bucket sort

0 likes • Nov 19, 2022 • 0 views
JavaScript
const bucketSort = (arr, size = 5) => {
const min = Math.min(...arr);
const max = Math.max(...arr);
const buckets = Array.from(
{ length: Math.floor((max - min) / size) + 1 },
() => []
);
arr.forEach(val => {
buckets[Math.floor((val - min) / size)].push(val);
});
return buckets.reduce((acc, b) => [...acc, ...b.sort((a, b) => a - b)], []);
};
bucketSort([6, 3, 4, 1]); // [1, 3, 4, 6]

Extracting components

0 likes • Nov 19, 2022 • 2 views
JavaScript
function formatDate(date) {
return date.toLocaleDateString();
}
function Comment(props) {
return (
<div className="Comment">
<div className="UserInfo">
<img
className="Avatar"
src={props.author.avatarUrl}
alt={props.author.name}
/>
<div className="UserInfo-name">
{props.author.name}
</div>
</div>
<div className="Comment-text">{props.text}</div>
<div className="Comment-date">
{formatDate(props.date)}
</div>
</div>
);
}
const comment = {
date: new Date(),
text: 'I hope you enjoy learning React!',
author: {
name: 'Hello Kitty',
avatarUrl: 'https://placekitten.com/g/64/64',
},
};
ReactDOM.render(
<Comment
date={comment.date}
text={comment.text}
author={comment.author}
/>,
document.getElementById('root')
);

LeetCode Two Sum

CHS
0 likes • Sep 13, 2023 • 2 views
JavaScript
/**
* @param {number[]} nums
* @param {number} target
* @return {number[]}
*/
var twoSum = function(nums, target) {
for(let i = 0; i < nums.length; i++) {
for(let j = 0; j < nums.length; j++) {
if(nums[i] + nums[j] === target && i !== j) {
return [i, j]
}
}
}
};

Heroku Production Build

CHS
0 likes • Mar 11, 2021 • 1 view
JavaScript
if (process.env.NODE_ENV === "production") {
app.use(express.static("client/build"));
app.get("*", (req, res) => {
res.sendFile(path.resolve(__dirname, "client", "build", "index.html"));
});
}