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 findIndexes = (arr, fn) => { | |
const res = []; | |
arr.map((el, i, arr) => { | |
if (fn(el, i, arr)) res.push(i); | |
}); | |
return res; | |
}; | |
// set prototype |
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
// code that generates BF (hardcoded) solution using js | |
// https://www.codewars.com/kata/bf-n-th-fibonacci-number/train/bf | |
const MAX = 100; | |
const flag = '+>'; | |
const inp = ','; | |
const nestedLoop = n => { | |
if (n > MAX) return ''; | |
return `-[ |
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 fib = n => { | |
const sq5 = Math.sqrt(5); | |
return (sq5 * (1+sq5)**n - sq5 * (1-sq5)**n) * 2**-n * 0.2; | |
}; | |
const fib2n = f => { | |
const sq5 = Math.sqrt(5); | |
const phi = (1 + sq5) / 2; | |
return Math.floor(Math.log(f * sq5 + 0.5) / Math.log(phi)); | |
}; |
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 bijectiveBinary = { | |
convertToInt: s => parseInt('1'+s.replace(/./g,d=>(+d)-1), 2) - 1, | |
convertFromInt: i => (i+1).toString(2).substr(1).replace(/./g,d=>(+d)+1) | |
}; | |
// codewars: | |
// https://www.codewars.com/kata/58f5e53e663082f9aa000060/solutions/javascript |
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 Sieve = (max = 1e5) => { | |
const maxi = Math.sqrt(max); | |
const notPrime = new Int8Array(max); | |
notPrime[0] = notPrime[1] = 1; | |
for (let i = 2; i < maxi; ++i) { | |
if (notPrime[i] === 0) { | |
for (let j = 2*i; j < max; j += i) { | |
notPrime[j] = 1; | |
} | |
} |
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
// input: array of bits | |
// [1,0,1,0,1,1] | |
function convertBit(p, v, i) { | |
return p[i] = i && p[i-1] ? 1 - v : v, p; | |
} | |
function bin2gray(bits) { | |
return bits.reduceRight(convertBit, bits); | |
} |
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 convertToRoman = num => { | |
const roman = [...'IVXLCDM']; | |
const patt = [ | |
[], | |
[0], | |
[0, 0], | |
[0, 0, 0], | |
[0, 1], | |
[1], | |
[1, 0], |
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 randomWord = (len = 16, up = true, low = true, digits = true, special = '') => { | |
const chars = [special]; | |
up && chars.push('ABCDEFGHIJKLMNOPQRSTUVWXYZ'); | |
low && chars.push('abcdefghijklmnopqrstuvwxyz'); | |
digits && chars.push('0123456789'); | |
const str = chars.join``; | |
const word = []; | |
for (let i = 0; i < len; ++i) { | |
word[i] = str[Math.round(Math.random() * str.length) % str.length]; |
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
// Smallest Common Multiple | |
// Least common multiple | |
// Greatest common divisor | |
function gcd(a, b) { | |
if (a < 0) a = -a; | |
if (b < 0) b = -b; | |
if (b > a) { | |
[a, b] = [b, a]; | |
} |
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 actions = [ | |
() => document.querySelector('button[name=delete]').click(), | |
() => document.querySelector('button.delete-address').click(), | |
() => document.querySelector('button.back').click() | |
]; | |
let i = 0; | |
setInterval(() => actions[(i++)%actions.length](), 1e3); |