Loading...
More JavaScript Posts
const euclideanDistance = (a, b) =>Math.hypot(...Object.keys(a).map(k => b[k] - a[k]));euclideanDistance([1, 1], [2, 3]); // ~2.2361euclideanDistance([1, 1, 1], [2, 3, 2]); // ~2.4495
/*** @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]}}}};
//Allegedly never tested on the twitter websitefunction scrapeScreen(){let articles = Array.from(document.getElementsByTagName("article"));let results = [];for(let tweet of articles){const isAnAd = Array.from(tweet.querySelectorAll("span")).map((e)=>e.textContent).includes("Ad");if(isAnAd){//console.log(tweet, "is an ad. Skipping...");continue;}const userName = tweet.querySelector("[data-testid='User-Name'] > div:nth-child(2) > div > div")?.textContent;const tweetContent = tweet.querySelector("[data-testid='tweetText']")?.textContent;const timeStamp = tweet.querySelector("time")?.getAttribute("datetime");const tweetLink = tweet.querySelector("time")?.parentElement?.getAttribute("href");if((!userName) || (!tweetContent)) continue;results.push({username: userName,tweetText: tweetContent,timeStamp: timeStamp,tweetLink: tweetLink});}return results;}let scraped = scrapeScreen();setInterval(()=>{scraped = scraped.concat(scrapeScreen().filter((tweet)=>{for(let scrapedTweet of scraped){if(scrapedTweet.username == tweet.username && scrapedTweet.tweetText == tweet.tweetText) return false;}return true;}));}, 500); //Scrape everything on the screen twice a secondwindow.scrollIntervalId = setInterval(function(){window.scrollBy(0, 1000);}, 500); //Scroll for me//http://bgrins.github.io/devtools-snippets/#console-save(function(console){console.save = function(data, filename){if(!data) {console.error('Console.save: No data')return;}if(!filename) filename = 'console.json'if(typeof data === "object"){data = JSON.stringify(data, undefined, '\t')}var blob = new Blob([data], {type: 'text/json'}),e = document.createEvent('MouseEvents'),a = document.createElement('a')a.download = filenamea.href = window.URL.createObjectURL(blob)a.dataset.downloadurl = ['text/json', a.download, a.href].join(':')e.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null)a.dispatchEvent(e)}})(console)setTimeout(()=>{clearTimeout(window.scrollIntervalId);delete window.scrollIntervalId;console.save(scraped, "TwitterScrape" + Date.now() + ".json");}, 60 * 1000 * 20); //Twenty minutes
class LinkedList {constructor() {this.nodes = [];}get size() {return this.nodes.length;}get head() {return this.size ? this.nodes[0] : null;}get tail() {return this.size ? this.nodes[this.size - 1] : null;}insertAt(index, value) {const previousNode = this.nodes[index - 1] || null;const nextNode = this.nodes[index] || null;const node = { value, next: nextNode };if (previousNode) previousNode.next = node;this.nodes.splice(index, 0, node);}insertFirst(value) {this.insertAt(0, value);}insertLast(value) {this.insertAt(this.size, value);}getAt(index) {return this.nodes[index];}removeAt(index) {const previousNode = this.nodes[index - 1];const nextNode = this.nodes[index + 1] || null;if (previousNode) previousNode.next = nextNode;return this.nodes.splice(index, 1);}clear() {this.nodes = [];}reverse() {this.nodes = this.nodes.reduce((acc, { value }) => [{ value, next: acc[0] || null }, ...acc],[]);}*[Symbol.iterator]() {yield* this.nodes;}}const list = new LinkedList();list.insertFirst(1);list.insertFirst(2);list.insertFirst(3);list.insertLast(4);list.insertAt(3, 5);list.size; // 5list.head.value; // 3list.head.next.value; // 2list.tail.value; // 4[...list.map(e => e.value)]; // [3, 2, 1, 5, 4]list.removeAt(1); // 2list.getAt(1).value; // 1list.head.next.value; // 1[...list.map(e => e.value)]; // [3, 1, 5, 4]list.reverse();[...list.map(e => e.value)]; // [4, 5, 1, 3]list.clear();list.size; // 0
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]
/*** @param {number[]} nums* @return {number[]}*/var productExceptSelf = function(nums) {var output = [];var leftMult = 1;var rightMult = 1;for (var i=nums.length - 1; i >= 0; i--) {output[i] = rightMult;rightMult *= nums[i];console.log({output: JSON.stringify(output), i})}for (var j=0; j < nums.length; j++) {output[j] *= leftMult;leftMult *= nums[j];console.log({output: JSON.stringify(output), j})}return output;};console.log(productExceptSelf([1, 2, 3, 4]))