Skip to content

Instantly share code, notes, and snippets.

@alysson-pina
alysson-pina / binaryFormat.js
Created December 29, 2019 13:48
Calculate Binary of a given number (in base 10)
/**
* Calculate binary form of a given number n.
*
* Recursive implementation to calculate binary number for a given number.
* Algorithm is O(log(N)) on both performance and memory consumption.
*
* @param {number} n Number in base 10
*
* @return {string} Binary representation of given number
*/
@alysson-pina
alysson-pina / flatten.js
Last active December 29, 2019 13:28
Flatten Array Of Integers without using .flat or .flatMap (also provided v2 that accepts arrays of any primitive value)
/**
* Exports function to flatten array of nested arrays into a single level array
* @param {Array} array of arrays of integers (any depth)
* @return {Array} array of integers (one level)
*/
const flatten = arr => arr.reduce( (acc, i) => `${acc + ' ' + i}` ).replace(/,/g, ' ').split(' ').map( i => parseInt(i, 10) );
export default flatten;