Skip to content

Instantly share code, notes, and snippets.

@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 / 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 / .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 / 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 / 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 / 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 / mergeRanges.js
Created August 12, 2016 23:20
merge ranges
/**
* Write a function mergeRanges that takes an array of meeting time ranges and#
*
* Example:
* var times = [[0, 1], [3, 5], [4, 8], [10, 12], [9, 10]
*
* mergeRanges(times); -> [[0, 1], [3, 8], [9, 12]]
*
* Do not assume the ranges are in order
*/
@jafow
jafow / rec.js
Created August 13, 2016 22:59
turtles all teh way down
function rockPaperScissors(num) {
var res = []
var t = ['rock', 'paper', 'scissors']
if (num < 1) return []
function rps (part, n) {
part = part || []
if (part.length === n) return res.push(part)
for (let i = 0, len = t.length; i < len; i++) {
@jafow
jafow / client.js
Created August 15, 2016 04:03
client
require('lookup-multicast-dns/global')
const net = require('net')
const jsonStream = require('duplex-json-stream')
const host = process.argv[3]
const nick = process.argv[2] || 'guest'
var socket = net.connect(9494, `${host}.local`)
socket = jsonStream(socket)