Skip to content

Instantly share code, notes, and snippets.

@specificityy
specificityy / flatten.js
Last active September 4, 2015 23:46
Function that flattens an array of arbitrarily nested arrays of integers into a flat array of integers. e.g. [[1,2,[3]],4] -> [1,2,3,4].
/**
* Flattens the passed in array.
*
* @param {Array} array The array to flatten.
* @param {boolean} isDeep Specify a deep flatten.
* @returns {Array} Returns the new flattened array.
*/
function flatten(array, isDeep) {
var index = -1,
length = array.length,
@specificityy
specificityy / isSumInArray.js
Last active September 4, 2015 23:48
Function that given a specific number (let's call it `n`) and an array of numbers (let's call it `numbers`) returns a Boolean, indicating if there is a pair of numbers in `numbers` that together add up to `n`. e.g.: n = 12, numbers = [1,2,3,4,5,6,7,8,9] -> true (because 5 + 7 == 12) e.g.2.: n = 20, numbers = [1,2,3,4,5,6] -> false
/**
* Checks if there's a pair of numbers in the array which add up to n.
*
* @param {Array} numbers The array to searched on.
* @param {number} n The number to be compared.
* @returns {boolean} Returns true if no matches are found.
*/
function isSumInArray(numbers, n) {
if (isNaN(n) || !numbers || !Array.isArray(numbers)) {
return undefined;