Awaits for a pending response with a single line.
const { status, data, error } = await parseResponse(fetch('/resource')))
const intersection = (a, b) => | |
a.map(ai => b.filter(bi => bi === ai)).flat() | |
intersection(['banana', 'apple'], ['banana', 'mellon']) // ['banana'] | |
intersection(['banana', 'apple'], ['banana', 'mellon', 'apple']) // ['banana', 'apple'] |
const grayscale = pixel => { | |
const [r, g, b] = pixel.data | |
const average = (r + g + b) / 3 | |
pixel.data[0] = average | |
pixel.data[1] = average | |
pixel.data[2] = average | |
return pixel | |
} |
const splitWords = phrase => phrase.split(' ') | |
const firstLetter = word => word.charAt(0) | |
const lastLetter = word => word.charAt(word.length - 1) | |
const initialLetter = words => words | |
.map(word => word.charAt(0)) | |
.join('') | |
const firstAndLastLetter = word => [ | |
firstLetter(word), |
Awaits for a pending response with a single line.
const { status, data, error } = await parseResponse(fetch('/resource')))
const twoDimensionsArray = [ | |
[-9, -9, -9, 1, 1, 1], | |
[ 0, -9, 0, 4, 3, 2], | |
[-9, -9, -9, 1, 2, 3], | |
[ 0, 0, 8, 6, 6, 0], | |
[ 0, 0, 0, -3, 0, 0], | |
[ 0, 0, 1, 2, 4, 0], | |
] | |
const twoDimensionsArray2 = [ |
const generate = (length = 1000) => Array(length).fill().map(() => | |
Math.floor(Math.random() * Math.floor(100)) | |
) |
function min(target: Array<Number>): Number { | |
const reducer = (lowerest, current) => current < lowerest ? current : lowerest | |
return target.reduce(reducer) | |
} | |
function max(target: Array<Number>): Number { | |
const reducer = (greatest, current) => current > greatest ? current : greatest | |
return target.reduce(reducer) | |
} |
function max(target: Array<Number>): Number { | |
const reducer = (greatest, current) => current > greatest ? current : greatest | |
return target.reduce(reducer) | |
} | |
function crush(length, queries) { | |
const zeroes = Array(length).fill(0) | |
for (let i = 0; i < queries.length; i++) { | |
const [a, b, k] = queries[i]; |
const fib = n => { | |
if (n <= 2) return 1 | |
return fib(n - 1) + fib(n - 2) | |
} | |
function fib2(n, mem = {}) { | |
let fibonacci; | |
if (n in mem) return mem[n] | |
fibonacci = n <= 2 ? 1 : fib2(n - 1, mem) + fib2(n - 2, mem) |
// O(logn) | |
const binarySearch = (list: number[], item) => { | |
let [from, to] = [0, list.length - 1]; | |
if (!item) return | |
while (from <= to) { | |
const index = Math.ceil((from + to) / 2); | |
const guess = list[index]; | |