This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function groupAnagrams(strs) { | |
// create a hash map to store the words and their sorted versions as the keys | |
let hash = {}; | |
// loop over the words | |
for (let word of strs) { | |
let cleansed = word.split("").sort().join(""); | |
// check if the sorted word is already in the map | |
if (hash[cleansed]) { | |
// if it is, add the word to the list of anagrams for that key |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// create a new set | |
let mySet = new Set(); | |
// add some values to the set | |
mySet.add(1); | |
mySet.add("hello"); | |
mySet.add({x: 10, y: 20}); | |
// check if a value is in the set | |
console.log(mySet.has(1)); // true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// create some sets | |
let setA = new Set([1, 2, 3, 4]); | |
let setB = new Set([3, 4, 5, 6]); | |
let setC = new Set([5, 6, 7, 8]); | |
// create a new set with the union of setA, setB, and setC | |
let unionSet = new Set([...setA, ...setB, ...setC]); | |
console.log(unionSet); // Set {1, 2, 3, 4, 5, 6, 7, 8} | |
// create a new set with the intersection of setA, setB, and setC |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const simpleHash = {} | |
simpleHash[1] = 'first' | |
simpleHash[2] = 'second' | |
simpleHash[3] = 'third' | |
console.log(simpleHash) // prints { '1': 'first', '2': 'second', '3': 'third' } | |
simpleHash[4] = 'fourth' | |
console.log(simpleHash) // prints { '1': 'first', '2': 'second', '3': 'third', '4': 'fourth' } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const hashmap = new Map() | |
hashmap.set(1, 'first') | |
hashmap.set(2, 'second') | |
hashmap.set(3, 'third') | |
console.log(' full ... ', hashmap) // Map(3) { 1 => 'first', 2 => 'second', 3 => 'third' } | |
console.log('size ... ', hashmap.size) // returns 3 | |
console.log('.get ... ', hashmap.get(2)) // returns second | |
console.log('.has ... ', hashmap.has(3)) // returns true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const twoSum = (dataSet, target) => { | |
const nums = {} | |
// Can also use Map() and will work the same | |
// const nums = new Map() | |
for (const num of dataSet) { | |
const potentialMatch = target - num | |
console.log(nums) | |
if(potentialMatch in nums) { | |
return [potentialMatch, num] // found match so return current num + potentialMatch |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react'; | |
import ReactDOM from 'react-dom'; | |
class App extends React.Component { | |
constructor(props) { | |
super(props); | |
this.state = { | |
example: 'test' | |
}; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "amazon-web-scaper", | |
"version": "1.0.0", | |
"description": "", | |
"main": "index.js", | |
"scripts": { | |
"test": "echo \"Error: no test specified\" && exit 1", | |
"dev": "nodemon -r esm index.js" | |
}, | |
"keywords": [], |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import axios from 'axios'; | |
import cherrio from 'cheerio'; | |
async function getHTML(productURL) { | |
const { data: html } = await axios.get(productURL, { | |
headers: { | |
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.90 Safari/537.36' | |
} | |
}) | |
.catch(function (error) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { getHTML, getAmazonPrice } from './scrape'; | |
const productURL = `https://www.amazon.ca/Vitamix-Explorian-Professional-Grade-Low-Profile-Refurbished/dp/B07CXVSMZ4/ref=sr_1_5?keywords=vitamix&qid=1555870204&s=gateway&sr=8-5&th=1`; | |
async function scrapePage() { | |
const html = await getHTML(productURL); | |
const amazonPrice = await getAmazonPrice(html); | |
console.log(`The price is ${amazonPrice}`); | |
} |
NewerOlder