Skip to content

Instantly share code, notes, and snippets.

@manobi
Created February 18, 2020 02:21
Show Gist options
  • Save manobi/d963e070c86f2641ed209209077c1838 to your computer and use it in GitHub Desktop.
Save manobi/d963e070c86f2641ed209209077c1838 to your computer and use it in GitHub Desktop.
pure smallEnought version
/**
* pure smallEnought version
* @param {array} a
* @param {number} limit
*/
const smallEnought = ([head, ...tail], limit) => (!head && !tail.length) || ((head > limit) ? false : smallEnought(tail, limit))
@manobi
Copy link
Author

manobi commented Feb 18, 2020

Extended version

const smallEnought = (a, limit) => {
  //destruct array
  const [head, ...tail] = a
  //stop positively when there nothing more to look for
  if(!head && !tail.length){
    return true
  }
  //if the first item of array is larger than limit return false otherwise call itself with the remaining elements
  return ((head > limit) ? false : smallEnought(tail, limit))
}

@manobi
Copy link
Author

manobi commented Feb 18, 2020

Tests

const assert = require('assert')
assert.equal(smallEnought([5, 4, 3, 2, 1], 4), false)
assert.equal(smallEnought([2, 4, 3, 2, 1], 4), true)
assert.equal(smallEnought([4, 4, 3, 2, 1], 4), true)
assert.equal(smallEnought([2, 4, 3, 2, 5], 4), false)
assert.equal(smallEnought([2, 7, 3, 2, 5], 4), false)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment