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
/** | |
* @param {any[]} array | |
* @param {any} x | |
* @param {any} y | |
*/ | |
function swap(array, x, y) { | |
temp = array[x]; | |
array[x] = array[y]; | |
array[y] = temp; | |
} |
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
export const useToggle = (defaultValue = false) => { | |
const [isEnabled, set] = useReducer( | |
(state, next) => (next == null ? !state : next), | |
defaultValue | |
); | |
const enable = () => set(true); | |
const disable = () => set(false); | |
const toggle = () => set(); |
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
type Stringified<T> = { [k in keyof T]: T[k] extends number ? string : T[k] }; | |
const stringifyWithKeys = <T, K extends keyof T>(obj: T, ...keys: K[]): Stringified<T> => { | |
const stringifiedObject = obj as Stringified<T>; | |
for (const [key, value] of Object.entries(pick(stringifiedObject, ...keys))) { | |
if (value !== undefined && typeof value === 'number') { | |
stringifiedObject[key as K] = String(value) as T[K] extends number ? string : T[K]; | |
} | |
} |
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
Object.mapEntries = (obj, callbackFn) => | |
Object.fromEntries( | |
Object.entries(obj).map(callbackFn) | |
) |
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
// 1. Генерация случайного числа в заданном диапазоне | |
const randomNumberInRange = (min, max) => Math.floor(Math.random() * (max - min + 1)) + min; | |
randomNumberInRange(10, 20) // 14 | |
// 2. Переключение логического значения | |
const toggle = value => !value | |
toggle(false) // true | |
// 3. Сортировка элементов массива в случайном порядке (осторожно! низкий уровень случайности) | |
const sortRandom = (arr) => arr.sort(() => Math.random() - 0.5) |
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
[ | |
{ | |
"type": "escape", | |
"width": 75, | |
"align": "left" | |
}, | |
{ | |
"type": "volumeDown", | |
"bordered": false, |
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
setTimeout(function() { | |
debugger; | |
}, 3000); |
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 If = foo => arg => foo(arg); | |
const True = arg1 => arg2 => arg1; | |
const False = arg1 => arg2 => arg2; | |
If(True)('one')('two'); // one | |
If(False)('one')('two'); // two |
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 partialApply = (fn, arg1) => (arg2, arg3) => fn(arg1, arg2, arg3); | |
const getAverageSalary = (profession, country) => { | |
if (profession === 'programmer') { | |
if (country === 'spain') return 2000; | |
if (country === 'russia') return 1200; | |
if (country === 'usa') return 8000; | |
} | |
} |
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
// Реализуйте и экспортируйте по умолчанию функцию apply, которая принимает на вход три параметра: | |
// Количество раз, которое нужно применить функцию к аргументу (ряд последовательных вызовов, где каждому следующему вызову передается аргумент, являющийся результатом предыдущего вызова функции; см. примеры ниже) | |
// Функцию для применения | |
// Аргумент для применения | |
// Рекурсивно: | |
function apply(n, foo, arg) { | |
if (n === 0) return arg; |
NewerOlder