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
return sum - Math.max.apply(Math, arr) + ', ' + sum - Math.min.apply(Math, arr); |
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
let sum = arr.reduce((p,c) => p + c, 0); | |
return `${sum - Math.max(...arr)}, ${sum - Math.min(...arr)}`; |
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 * throttle(func, time) { | |
let timerID = null; | |
function throttled(arg) { | |
clearTimeout(timerID); | |
timerID = setTimeout(func.bind(window, arg), time); | |
} | |
while (true) | |
throttled(yield); | |
} |
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 * fibonacci(seed1, seed2) { | |
while (true) { | |
yield (() => { | |
seed2 = seed2 + seed1; | |
seed1 = seed2 - seed1; | |
return seed2; | |
})(); | |
} | |
} |
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 * iterateArray(arr) { | |
for (let i in arr) | |
yield arr[i]; | |
} | |
const iterArr = iterateArray([0,1,2]); | |
iterArr.next(); // {value: 0, done: false} | |
iterArr.next(); // {value: 1, done: false} | |
iterArr.next(); // {value: 2, done: false} | |
iterArr.next(); // {value: undefined, done: 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
function * randomFrom(...arr) { | |
while (true) | |
yield arr[Math.floor(Math.random() * arr.length)]; | |
} | |
const getRandom = randomFrom(1, 2, 5, 9, 4); | |
getRandom.next().value; // returns random value |
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 * generator(arr) { | |
for (const i in arr) { | |
yield i; | |
yield yield; | |
yield(yield); | |
} | |
} | |
const gen = generator([0,1]); |
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
for (let i = 0; i < 5; i += 1) { | |
console.log(i); | |
} | |
// this will return immediately 0 -> 1 -> 2 -> 3 -> 4 |
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 * generatorForLoop(num) { | |
for (let i = 0; i < num; i += 1) { | |
yield console.log(i); | |
} | |
} | |
const genForLoop = generatorForLoop(5); | |
genForLoop.next(); // first console.log - 0 | |
genForLoop.next(); // 1 |
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 * generator () {} | |
function* generator () {} | |
function *generator () {} | |
let generator = function * () {} | |
let generator = function* () {} | |
let generator = function *() {} | |
let generator = *() => {} // SyntaxError | |
let generator = ()* => {} // SyntaxError |