This file contains hidden or 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
// Sorting with Array.prototype.filter() | |
const startWith0 = (arr) => [...arr.filter(x => x === 0), ...arr.filter(x => x !== 0)]; | |
// Sorting with Array.prototype.sort() | |
// const startWith0 = (arr) => arr.sort((x) => x === 0 ? -1 : 1); | |
console.time('time'); | |
console.log('result', startWith0([1, null, 'e', 0, 2, 'a', 123, undefined, false, 0, true, 'd', 0, true])); | |
console.timeEnd('time'); |
This file contains hidden or 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
// this is wrong | |
for (var i = 0; i < 5; i++) { | |
setTimeout(() => console.log(i), 1000); // => 5 5 5 5 5 | |
} | |
// easy fix to use let instead of var. block level scoping instead of function level | |
for (let i = 0; i < 5; i++) { | |
setTimeout(() => console.log(i), 1000); // => 0 1 2 3 4 | |
} |
This file contains hidden or 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 pause = (data, time) => new Promise(resolve => setTimeout(() => resolve(data), time)); | |
const sequentialParallel = async () => { | |
console.time('time'); | |
// Sequential | |
// const first = await pause('first', 3000); | |
// const second = await pause('second', 2000); | |
// Parallel |
This file contains hidden or 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 numbers = [1, 2, 3, 4, 12, 50]; | |
const reducer = (accumulator, currentValue) => accumulator + currentValue; | |
const reducedNumbers = numbers.reduce((accumulator, currentValue) => accumulator + currentValue); | |
const multipliedNumbers = numbers.map(x => x * 2); | |
console.log(numbers.reduce(reducer)); // => 72 | |
console.log(reducedNumbers); // => 72 | |
console.log(multipliedNumbers); // => [ 2, 4, 6, 8, 24, 100 ] |
This file contains hidden or 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
// How to add and retrieve values in js with ES5 | |
const counterES5 = () => { | |
let storedNumber = 0; | |
return { | |
add: function(newNumber) { | |
storedNumber += newNumber; | |
}, | |
retrieve: function() { | |
return storedNumber; |
This file contains hidden or 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
// Higher-Order Functions with ES5 | |
function createBaseES5(x) { | |
return function (y) { | |
return x + y; | |
} | |
} | |
const addTwo = createBaseES5(2); | |
console.log(addTwo(12)); // => 14 | |
console.log(addTwo(23)); // => 25 |
This file contains hidden or 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
// Not recommended - too sequential | |
async function logInOrder(urls) { | |
for (const url of urls) { | |
const response = await fetch(url); | |
console.log(await response.text()); | |
} | |
} | |
// Recommended - nice and parallel | |
async function logInOrder(urls) { |
This file contains hidden or 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
// Copying an array | |
let copy = [...arr] | |
// Creating an array of unique elements | |
let uniqueElements = [...new Set(arr)]; | |
let elementsStartingWithA = [...new Set(arr)].filter(e => e.startsWith('a')); | |
// Concatenate arrays | |
let concat = [...arr1, ...arr2]; |
This file contains hidden or 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 fetchAndUpdatePosts = () => { | |
fetchPosts() | |
.then((posts) => { | |
updatePosts(posts) | |
.catch((err) => { | |
console.log('error in updating posts'); | |
}); | |
}) | |
.catch(() => { | |
console.log('error in fetching posts'); |
This file contains hidden or 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
@keyframes listItemFadeIn { | |
0% { | |
opacity: 0; | |
} | |
100% { | |
opacity: 1; | |
margin-top: 0px; | |
} | |
} |