Skip to content

Instantly share code, notes, and snippets.

@jafow
jafow / combo.js
Last active July 28, 2016 20:29
Combo
/** Write a function `combo` that takes two parameters--fn1, fn2--and returns a function.
* combo should combine the two function parameters and invoke them on any values passed as arguments
* ex:
* var add2 = (n) => n + 2;
* var times3 = (n) => n * 3;
* var plus2AndMultiply3 = combo(add2, times/3)
* plus2AndMulitply3(5) // --> 17
*/
// Function composition in Es6
const combo = (fn1, fn2) => (...args) => fn1(fn2(args));
@jafow
jafow / targetSum.js
Last active August 4, 2016 18:58
targetSum.js
// define a function 'targetSum' that takes 2 arguments, an array and a target number, and returns a boolean for whether or not
//2 values in the array sum up to the target number
// ex: targetSum([1, 3, 5, 0, 6, 4, 2], 11) // => 11
// brute force
function targetSum (arr, target) {
for (var i = 0; i < arr.length; i++) {
for (var j = i + 1; j < arr.length; j++) {
if (arr[i] + arr[j] === target) return true
}
@jafow
jafow / Row.js
Created July 3, 2016 05:32
React component that receives, extends, and transfers props
class Row extends Component {
// this component receives props and extends it with color and effect. `super()` keyword permits us to use `this`
constructor(props) {
super(props)
this.color = 'blue'
this.effect = 'wow'
}
render () {
return (
@jafow
jafow / pollingData.js
Last active June 30, 2016 02:54
polling data
var presidentialPollingData = [{"pollster":"RCP Average","date":"6/27","clinton":45,"trump":39},
{"pollster":"Quinnipiac","date":"6/27","clinton":42,"trump":40},{"pollster":"ABC News/Wash Post","date":"6/23","clinton":51,"trump":39},
{"pollster":"NBC News/Wall St. Jrnl","date":"6/23","clinton":46,"trump":41},{"pollster":"Rasmussen Reports","date":"6/21","clinton":44,"trump":39},
{"pollster":"Reuters/Ipsos","date":"6/22","clinton":44,"trump":34},{"pollster":"Economist/YouGov","date":"6/20","clinton":43,"trump":39},
{"pollster":"CNN/ORC","date":"6/19","clinton":47,"trump":42},{"pollster":"Gravis","date":"6/16","clinton":51,"trump":49},
{"pollster":"Monmouth","date":"6/19","clinton":49,"trump":41},{"pollster":"Rasmussen Reports","date":"6/15","clinton":44,"trump":39},
{"pollster":"Reuters/Ipsos","date":"6/15","clinton":41,"trump":32},{"pollster":"CNBC","date":"6/13","clinton":40,"trump":35},
{"pollster":"Bloomberg*","date":"6/13","clinton":49,"trump":37},{"pollster":"CBS News","date":"6/13","clinton":43,"trump":
@jafow
jafow / .gitconfig
Last active June 22, 2016 22:08
git aliases
[user] name = jafow
email = jared.a.fowler@gmail.com
[credential]
helper = cache
help = cache --timeout 1800
[core]
autocrlf = input
safecrlf = true
editor = vim
@jafow
jafow / divorce.js
Created June 15, 2016 01:53
divorce
/**
* divorce takes an array of nested array pairs and returns 2 one-dimensional arrays with elements from each pair, in order
* ex: divorce([[1,2], [3,4]]) --> {a1: [1,3], a2: [2,4]}
*/
const divorce = (arr, a1 = [], a2 = []) =>
!arr[0]
? { a1, a2 }
: divorce(arr.slice(1), a1.concat(arr[0][0]), a2.concat(arr[0][1]))
@jafow
jafow / cartwheel.js
Created June 15, 2016 01:32
cartwheel
/**
* input: @arr array
* @out array
* @cycles integer
* output: @out, array of @cycles repetions of @arr
* ex: cartwheel([1,2,3], 3) --> [1,2,3,1,2,3,1,2,3]
*/
const cartwheel = (arr, out = [], cycles) =>
cycles === 0
@jafow
jafow / getLength.js
Last active June 16, 2016 00:46
getLength
// Write a fn `getLength` that takes one argument, an array, and returns the length of that array.
// getLength should be written without using the built-in .length() method, and without any `for` or `while` loops.
const getLength = ([first, ...rest], len = 0) =>
!first
? len
: getLength(rest, len + 1)
// ES5
@jafow
jafow / delay.js
Created April 24, 2016 20:40
Example implementations of delay
function delay (fn, wait) {
var args = Array.prototype.slice.call(arguments, 2)
setTimeout(function () {
return fn.apply(this, args)
}, wait)
}
// with ES6 rest operator instead of slicing arguments object
const delay2 = (fn, wait, ...args) => {
setTimeout(() => fn.apply(this, args), wait)
@jafow
jafow / once.js
Created April 24, 2016 20:14
Example once implementation
/** Once takes a function as an argument and returns a function or undefined, depending on whether the fn passed as an argument
* has been called. If that function hasn't been invoked, Once will return the value of that function applied to any arguments.
* otherwise it will return undefined.
*/
// implementation #1
function once (fn) {
var run = false
return function () {
if (run) return void 0