├── pt6-copyWithin.js ├── pt4-array-from.js ├── pt3-filter-hasNext.js ├── pt1-forEach.js ├── pt4-concat.js ├── pt2-some-every.js ├── pt3-filter-objects.js ├── pt2-destructuring.js ├── pt1-for.js ├── pt6-splice.js ├── pt1-for-of-in.js ├── pt3-slice.js ├── pt2-find.js ├── pt3-filter-numbers.js ├── pt2-find-indexes.js ├── pt6-shift.js ├── pt5-async.js ├── pt2-copies.js ├── pt4-flat.js ├── pt5-entries.js ├── pt1-async.js ├── pt5-mother.js ├── pt5-reduce-to-object.js ├── pt6-sort.js ├── pt6-push.js ├── pt5-reduce-to-value.js ├── pt5-reduce-to-array.js └── pt4-map.js /pt6-copyWithin.js: -------------------------------------------------------------------------------- 1 | const numbers = [1, 2, 3, 4, 5]; 2 | 3 | numbers.copyWithin(2, 0); 4 | numbers; 5 | -------------------------------------------------------------------------------- /pt4-array-from.js: -------------------------------------------------------------------------------- 1 | const numbers = [10, 20, 30, 40, 50]; 2 | 3 | Array.from(numbers); 4 | Array.from(numbers, (v) => v * 10); 5 | -------------------------------------------------------------------------------- /pt3-filter-hasNext.js: -------------------------------------------------------------------------------- 1 | const numbers = [9, 10, 11, 13, 14, 15]; 2 | 3 | const hasNext = numbers.filter((v, _, a) => a.includes(v + 1)); 4 | hasNext; 5 | -------------------------------------------------------------------------------- /pt1-forEach.js: -------------------------------------------------------------------------------- 1 | const numbers = [10, 20, 30, 40, 50]; 2 | 3 | numbers.forEach((value, index) => { 4 | console.log(value); 5 | console.log(index); 6 | }); 7 | -------------------------------------------------------------------------------- /pt4-concat.js: -------------------------------------------------------------------------------- 1 | const first = [10, 20]; 2 | const second = [30, 40, 50]; 3 | 4 | first.concat(second); 5 | 6 | first.concat(second).map((v) => v * 10); 7 | 8 | [...first, ...second].map((v) => v * 10); 9 | -------------------------------------------------------------------------------- /pt2-some-every.js: -------------------------------------------------------------------------------- 1 | const numbers = [10, -20, 30, -40, 50]; 2 | 3 | console.log(numbers.includes(10)); 4 | 5 | console.log(numbers.some((num) => num > 0)); 6 | console.log(numbers.every((num) => num > 0)); 7 | -------------------------------------------------------------------------------- /pt3-filter-objects.js: -------------------------------------------------------------------------------- 1 | const people = [{ name: "John" }, { name: "Ann" }]; 2 | 3 | const jPeople = people.filter(({ name }) => name.startsWith("J")); 4 | jPeople; 5 | jPeople[0].name = "Jack"; 6 | jPeople; 7 | people; 8 | -------------------------------------------------------------------------------- /pt2-destructuring.js: -------------------------------------------------------------------------------- 1 | const numbers = [10, 20, 30, 40, 50]; 2 | // const first = numbers[0]; 3 | // const second = numbers[1]; 4 | const [first, second, third, ...rest] = numbers; 5 | first; 6 | second; 7 | third; 8 | rest; 9 | -------------------------------------------------------------------------------- /pt1-for.js: -------------------------------------------------------------------------------- 1 | // good 2 | const arrayOfNumbers = []; 3 | for (let value = 10; value <= 50; value += 10) { 4 | arrayOfNumbers.push(value); 5 | } 6 | 7 | // bad 8 | for (let index = 0; index < arrayOfNumbers.length; index++) { 9 | console.log(arrayOfNumbers[index]); 10 | } 11 | -------------------------------------------------------------------------------- /pt6-splice.js: -------------------------------------------------------------------------------- 1 | const values = [1, 2, 4, 5, 6]; 2 | 3 | values; 4 | values.splice(2, 0, 3); 5 | values; 6 | values.splice(2, 1); 7 | values; 8 | 9 | const unchanging = [1, 2, 4, 5, 6]; 10 | const copy = [...unchanging]; 11 | copy.splice(2, 0, 3); 12 | copy; 13 | unchanging; 14 | -------------------------------------------------------------------------------- /pt1-for-of-in.js: -------------------------------------------------------------------------------- 1 | const numbers = [10, 20, 30, 40, 50]; 2 | 3 | for (const index in numbers) { 4 | console.log(index); 5 | console.log(numbers[index]); 6 | } 7 | 8 | for (const value of numbers) { 9 | if (value > 20) { 10 | break; 11 | } 12 | console.log(value); 13 | } 14 | -------------------------------------------------------------------------------- /pt3-slice.js: -------------------------------------------------------------------------------- 1 | const numbers = [10, 20, 30, 40, 50]; 2 | 3 | const middleThree = numbers.filter((_, i) => i > 0 && i < 4); 4 | middleThree; 5 | 6 | const easierMiddleThree = numbers.slice(1, 4); 7 | easierMiddleThree; 8 | 9 | numbers.slice(); 10 | numbers.slice(1); 11 | numbers.slice(-1); 12 | numbers.slice(-2); 13 | -------------------------------------------------------------------------------- /pt2-find.js: -------------------------------------------------------------------------------- 1 | const names = ["Alice", "Bob", "Tiff", "Bruce", "Alice"]; 2 | 3 | console.log(names.find((value) => value === "Alice")); 4 | 5 | const people = [{ name: "John" }, { name: "Jane" }]; 6 | const person = people.find((p) => p.name === "Jane"); 7 | person; 8 | person.name = "Sally"; 9 | person; 10 | people; 11 | -------------------------------------------------------------------------------- /pt3-filter-numbers.js: -------------------------------------------------------------------------------- 1 | const numbers = [10, -20, 30, -40, 50]; 2 | 3 | const allPositive = numbers.filter((v) => v > 0); 4 | allPositive; 5 | const allNegatives = numbers.filter((v) => v < 0); 6 | allNegatives; 7 | 8 | const positiveUnder50 = numbers.filter((v) => v > 0).filter((v) => v < 50); 9 | positiveUnder50; 10 | -------------------------------------------------------------------------------- /pt2-find-indexes.js: -------------------------------------------------------------------------------- 1 | const names = ["Alice", "Bob", "Tiff", "Bruce", "Alice"]; 2 | 3 | console.log(names.indexOf("Alice")); 4 | console.log(names.indexOf("Alice", 1)); 5 | console.log(names.indexOf("Sally")); 6 | 7 | console.log(names.lastIndexOf("Alice")); 8 | 9 | console.log(names.findIndex((name) => name === "Bruce")); 10 | -------------------------------------------------------------------------------- /pt6-shift.js: -------------------------------------------------------------------------------- 1 | const original = [1, 2, 3, 4, 5]; 2 | 3 | original; 4 | original.shift(); 5 | original; 6 | original.unshift(1); 7 | original; 8 | 9 | const unchanging = [1, 2, 3, 4, 5]; 10 | // shift 11 | const [value, ...rest] = unchanging; 12 | value; 13 | rest; 14 | // unshift 15 | const newArray = [0, ...unchanging]; 16 | newArray; 17 | unchanging; 18 | -------------------------------------------------------------------------------- /pt5-async.js: -------------------------------------------------------------------------------- 1 | function getById(id) { 2 | return new Promise((resolve) => { 3 | setTimeout(() => { 4 | console.log(`Got ${id}`); 5 | resolve(id); 6 | }, 1000); 7 | }); 8 | } 9 | 10 | const ids = [10, 20, 30]; 11 | 12 | ids.reduce(async (promise, id) => { 13 | await promise; 14 | return getById(id); 15 | }, Promise.resolve()); 16 | -------------------------------------------------------------------------------- /pt2-copies.js: -------------------------------------------------------------------------------- 1 | const numbers = [10, 20, 30, 40, 50]; 2 | numbers; 3 | const copyOfNumbers = [...numbers]; 4 | copyOfNumbers; 5 | copyOfNumbers[0] = 100; 6 | copyOfNumbers; 7 | numbers; 8 | 9 | const people = [{ name: "John" }, { name: "Jane" }]; 10 | people; 11 | const copyOfPeople = [...people]; 12 | copyOfPeople; 13 | copyOfPeople[0].name = "Jack"; 14 | copyOfPeople; 15 | people; 16 | -------------------------------------------------------------------------------- /pt4-flat.js: -------------------------------------------------------------------------------- 1 | const numbers = [ 2 | [10, 20, 30], 3 | [40, 50, 60], 4 | [70, 80, 90], 5 | ]; 6 | numbers.flat(); 7 | 8 | const deepNumbers = [[[[10, 20, 30]]], [[[40, 50, 60]]], [[[70, 80, 90]]]]; 9 | deepNumbers.flat(); 10 | deepNumbers.flat(2); 11 | deepNumbers.flat(Infinity); 12 | 13 | const values = [10, 20, 30, 40, 50]; 14 | 15 | values.map((v, i) => [v, i]); 16 | values.flatMap((v, i) => [v, i]); 17 | -------------------------------------------------------------------------------- /pt5-entries.js: -------------------------------------------------------------------------------- 1 | const values = [10, 20, 30, 40, 50]; 2 | 3 | values.entries(); 4 | for (const value of values.values()) { 5 | console.log(value); 6 | } 7 | 8 | const customers = { 9 | Jack: 12, 10 | Jim: 15, 11 | Sally: 18, 12 | }; 13 | 14 | function sum(objOrArray) { 15 | return Object.values(objOrArray).reduce((sum, value) => sum + value, 0); 16 | } 17 | 18 | sum(values); 19 | sum(customers); 20 | -------------------------------------------------------------------------------- /pt1-async.js: -------------------------------------------------------------------------------- 1 | function getById(id) { 2 | return new Promise((resolve) => { 3 | setTimeout(() => { 4 | console.log(`Got ${id}`); 5 | resolve(id); 6 | }, 1000); 7 | }); 8 | } 9 | 10 | (async function () { 11 | const ids = [10, 20, 30]; 12 | // for (const id of ids) { 13 | // await getById(id); 14 | // } 15 | ids.forEach(async (id) => { 16 | await getById(id); 17 | }); 18 | })(); 19 | -------------------------------------------------------------------------------- /pt5-mother.js: -------------------------------------------------------------------------------- 1 | const numbers = [1, 2, 3, 4, 5]; 2 | 3 | // Includes 4 | numbers.reduce((includes, value) => (includes ? includes : value === 3), false); 5 | numbers.reduce( 6 | (includes, value) => (includes ? includes : value === 10), 7 | false 8 | ); 9 | 10 | // Slice 11 | numbers.reduce( 12 | (arr, value, index) => (index > 0 && index < 4 ? [...arr, value] : arr), 13 | [] 14 | ); 15 | 16 | // Map 17 | numbers.reduce((arr, value) => [...arr, value * 100], []); 18 | -------------------------------------------------------------------------------- /pt5-reduce-to-object.js: -------------------------------------------------------------------------------- 1 | const numbers = [12, 15, 12, 2, 6, 6, 2, 12]; 2 | 3 | const lookup = {}; 4 | for (const number of numbers) { 5 | lookup[number] = (lookup[number] ?? 0) + 1; 6 | } 7 | lookup; 8 | 9 | numbers.reduce( 10 | (lookup, value) => ({ 11 | ...lookup, 12 | [value]: (lookup[value] ?? 0) + 1, 13 | }), 14 | {} 15 | ); 16 | 17 | numbers.reduce( 18 | ({ min, max }, value) => ({ 19 | min: Math.min(min, value), 20 | max: Math.max(max, value), 21 | }), 22 | { 23 | min: Infinity, 24 | max: -Infinity, 25 | } 26 | ); 27 | -------------------------------------------------------------------------------- /pt6-sort.js: -------------------------------------------------------------------------------- 1 | const numbers = [2, 6, 3, 4, 1, 5]; 2 | numbers.sort(); 3 | 4 | const names = ["Jack", "Jill", "Jane", "John", "Jim"]; 5 | names.sort(); 6 | 7 | const people = [ 8 | { id: 6, name: "Jack" }, 9 | { id: 1, name: "Sam" }, 10 | { id: 3, name: "Sally" }, 11 | ]; 12 | people.sort((a, b) => a.id - b.id); 13 | people; 14 | 15 | const unchanging = [2, 6, 3, 4, 1, 5]; 16 | const newNumbers = [...unchanging].sort(); 17 | unchanging; 18 | 19 | numbers; 20 | numbers.reverse(); 21 | numbers; 22 | 23 | unchanging; 24 | [...unchanging].sort().reverse(); 25 | unchanging; 26 | -------------------------------------------------------------------------------- /pt6-push.js: -------------------------------------------------------------------------------- 1 | const original = [1, 2, 3, 4, 5]; 2 | 3 | const originalRef = original; 4 | originalRef === original; 5 | 6 | original; 7 | original.pop(); 8 | original; 9 | original.push(5); 10 | original; 11 | 12 | originalRef === original; 13 | 14 | let aNumber = 5; 15 | const stateManagerCopy = aNumber; 16 | stateManagerCopy === aNumber; 17 | aNumber = 6; 18 | stateManagerCopy === aNumber; 19 | 20 | const unchanging = [1, 2, 3, 4, 5]; 21 | // pop 22 | const popped = unchanging.slice(-1)[0]; 23 | const rest = unchanging.slice(0, -1); 24 | // push 25 | const aNewArray = [...unchanging, 6]; 26 | -------------------------------------------------------------------------------- /pt5-reduce-to-value.js: -------------------------------------------------------------------------------- 1 | const numbers = [10, 20, 30, 40, 50]; 2 | 3 | let sum = 0; 4 | for (const value of numbers) { 5 | sum += value; 6 | } 7 | sum; 8 | 9 | numbers.reduce((sum, value) => { 10 | sum += value; 11 | return sum; 12 | }, 0); 13 | 14 | numbers.reduce((sum, value) => sum + value, 0); 15 | 16 | numbers.reduce((avg, value, _, arr) => avg + value / arr.length, 0); 17 | 18 | const names = ["LG", "Mimi", "Sadie", "Ripley"]; 19 | 20 | const str = names.reduce( 21 | (str, name, index) => str + (index > 0 ? ", " : "") + name, 22 | "" 23 | ); 24 | str; 25 | 26 | names.join(", "); 27 | -------------------------------------------------------------------------------- /pt5-reduce-to-array.js: -------------------------------------------------------------------------------- 1 | const numbers = [1, 2, 3, 4, 5]; 2 | 3 | let arr = []; 4 | for (const number of numbers) { 5 | arr = [number, ...arr]; 6 | } 7 | arr; 8 | 9 | numbers.reduce((arr, number) => [...arr, number], []); 10 | 11 | numbers.reduceRight((arr, number) => [...arr, number], []); 12 | 13 | const groups = [ 14 | [3, 2], 15 | [2, 5], 16 | [3, 7], 17 | ]; 18 | 19 | // [2,2,2,5,5,7,7,7] 20 | 21 | groups.reduce((arr, [count, value]) => { 22 | for (let index = 0; index < count; index++) { 23 | arr.push(value); 24 | } 25 | return arr; 26 | }, []); 27 | 28 | []; 29 | Array(3); 30 | Array(3).fill(2); 31 | 32 | groups.reduce( 33 | (arr, [count, value]) => [...arr, ...Array(count).fill(value)], 34 | [] 35 | ); 36 | -------------------------------------------------------------------------------- /pt4-map.js: -------------------------------------------------------------------------------- 1 | const numbers = [10, 20, 30, 40, 50]; 2 | 3 | const numbersTimes10 = numbers.map((v) => v * 10); 4 | numbersTimes10; 5 | 6 | const numbersTimes10Obj = numbers.map((v) => ({ value: v * 10 })); 7 | numbersTimes10Obj; 8 | 9 | const numbersWithNegatives = [-10, 20, 30, -40, -50]; 10 | const positveBy10 = numbersWithNegatives 11 | .filter((v) => v > 0) 12 | .map((v) => v * 10); 13 | positveBy10; 14 | 15 | const people = [ 16 | { first: "Jane", last: "Smith", address: { city: "Oakland" } }, 17 | { first: "Sally", last: "Joe", address: { city: "Foster City" } }, 18 | ]; 19 | 20 | const cheapClone = (obj) => JSON.parse(JSON.stringify(obj)); 21 | 22 | const fullNames = people.map((p) => 23 | cheapClone({ 24 | ...p, 25 | fullName: `${p.first} ${p.last}`, 26 | }) 27 | ); 28 | fullNames; 29 | fullNames[0].first = "Penny"; 30 | fullNames[0].address.city = "San Jose"; 31 | fullNames; 32 | people; 33 | --------------------------------------------------------------------------------