Skip to content

Instantly share code, notes, and snippets.

View gartenfeld's full-sized avatar

David Rosson gartenfeld

View GitHub Profile
@gartenfeld
gartenfeld / async_map.js
Created October 27, 2015 07:45
Async mapping.
var asyncMap = function(tasks, finalCb){
var results = [];
var completed = 0;
var checkCompleteness = function() {
if (completed === tasks.length) {
finalCb(results);
}
};
var runTask = function (i) {
var onResult = function(val) {
@gartenfeld
gartenfeld / circular_skipping.js
Created October 14, 2015 01:13
A space-intensive solution...
// N number of people stand in a circle.
// The people are numbered in order from 1 to N.
// Starting from 1, we remove every other person
// from the circle until there is only one person remaining.
// I would like you to write a function which takes in the number N
// and outputs the number of the last person remaining. For example:
// f(3) outputs 3
// f(4) outputs 1
// f(5) outputs 3
// initiating a worker
var karl = new Worker("task.js");
// receiving messages
karl.onmessage = function(event){
console.log(event.data);
};
// in the end
karl.terminate();
@gartenfeld
gartenfeld / curb.js
Created October 10, 2015 19:49
Solving a puzzle with async depth-first traversal.
var request = require('request');
var Promise = require('bluebird');
var authorize = require('./session-keys');
var BASE_URL = 'http://challenge.shopcurbside.com/';
var fetch = function(resource, session) {
var options = {
url: BASE_URL + resource,
headers: { 'Session': session }
@gartenfeld
gartenfeld / base_convert.js
Created October 6, 2015 04:07
Converting from Base 10.
var num = "27019",
base = 16,
output = '';
while (num > 0) {
digit = num % base;
num = (num - digit) / base;
if (digit > 9) {
digit = String.fromCharCode(digit + 55);
}
@gartenfeld
gartenfeld / preset_array.js
Last active October 3, 2015 21:49
An alternative to .push()
function solution(A) {
var n = A.length - 1,
lSum = 0,
rSum = 0,
diffs = new Array(n),
diff,
min = Infinity;
for (var p = 0; p < n; p++) {
lSum += A[p];
rSum += A[n - p];
@gartenfeld
gartenfeld / text_wrap.js
Created September 14, 2015 16:58
Wrapping a long string into a paragraph with line-breaks.
function wrap (text, limit) {
if (text.length > limit) {
// find the last space within limit
var edge = text.slice(0, limit).lastIndexOf(' ');
if (edge > 0) {
var line = text.slice(0, edge);
var remainder = text.slice(edge + 1);
return line + '\n' + wrap(remainder, limit);
}
}
@gartenfeld
gartenfeld / module.js
Created August 31, 2015 21:31
Creating new closure scopes in Node.
module.exports = function () {
var c = [];
return {
push: function (item) {
c.push(item);
},
print: function () {
console.log(c);
}
};
@gartenfeld
gartenfeld / trigger_handlers.js
Created August 28, 2015 18:24
Organizing handler functions using a hash.
var handlers = {
onConnect: function () {
console.log('Connected!');
},
onMessage: function () {
console.log('Message!');
}
};
var map = {