Skip to content

Instantly share code, notes, and snippets.

@jafow
jafow / euclidsAlgo.md
Last active October 11, 2015 14:43
Euclid's Algorithm on JavaScript

####What's the greatest common divisor of two numbers?

'use strict';

let euclid = (int1, int2) {
  
  if(int1 === int2)
    return int1;

 if(int1 > int2)
@jafow
jafow / simple-compose.js
Created December 8, 2015 05:59
simple example of compose
const makeUpperCase = (word) => word.toUpperCase();
const makePlural = (word) => word + 's';
makePlural('shoe');//=> 'shoes'
makeUpperCase('shoe');//=> 'SHOE'
const compose = (x, y) => {
return function (z) {
return x(y(z));
@jafow
jafow / gist:13bb3c1e94a0c38eeaf2
Last active January 14, 2016 02:35
placing an avatar and image feed
$( document ).ready( function () {
$.getJSON("#dataobject", (data) => {
let imgArray = matchElements(data)(/(?:scontent)/);
//place and style the user avatar thumbnail
placeAvatar(".img-thumbnail", imgArray[0]);
//display the user's feed
$( ".img-display" ).html("<img src='" + imgArray[1] + "'/>");
@jafow
jafow / gist:72fb30c47aaf497a31a7
Created January 14, 2016 02:38
matchElements
/**Some of the image URLs in the datastore are broken. This ```matchElements``` function returns a new array of valid URLs that match the ``@param {Regex} match` object.
*/
const matchElements = (arr) =>
function (match) {
return arr.filter((str) => match.test(str))
}
var reset = document.getElementById('reset');
reset.addEventListener('click', function(e) {
location.reload();
});
@jafow
jafow / memoize.js
Last active April 24, 2016 21:09
Example memoize implementations
/** Memoize takes a function as an argument and returns a function. When the supplied function is called, it is stored in a
* lookup table. That way the next time it's called the passed function won't run again and will instead return the stored value.
*/
function memoize (fn) {
var cache {}
return function () {
var args = Array.prototype.slice.call(arguments)
var key = JSON.stringify(args)
if (key in cache) return cache[key]
else {
@jafow
jafow / fun-start.js
Created March 3, 2016 03:00
quick wt start
var wt = require('webtorrent');
var client = new wt();
client.seed(__dirname + '/astro.jpg', function (torrent) {
console.log('seeding the file ', torrent.infoHash);
});
@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
@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 / 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