Skip to content

Instantly share code, notes, and snippets.

function getRandomSex() {
return (Math.random() > 0.50) ? 'male' : 'female';
}
/**
* @private
* @returns {String}
*/
function generateName(gender, parents = []) {
}
@codyromano
codyromano / topo.js
Last active December 28, 2017 14:02
const hasIncomingEdges = node => node.edges.length;
const noIncomingEdges = node => !node.edges.length;
function removeEdge(adjacentVertex, node) {
node.edges = node.edges.filter(vertex => vertex !== adjacentVertex);
return node;
}
function topologicalSort(nodes = []) {
let noEdges = nodes.filter(noIncomingEdges),
/**
* @desc Find the k most frequently occurring words in a body of text.
*
* @param {String} paragraph
* @param {Integer} k
* @returns {Array}
*/
function getMostFrequentWords(paragraph = '', k = 0) {
if (typeof paragraph !== 'string' || !Number.isInteger(k) || k < 0) {
throw new TypeError('Invalid paragraph text or frequency count.');
=> [ 'the',
'and',
'a',
'of',
'like',
'with',
'forests',
'winds',
'every',
'as' ]
getMostFrequentWords(`THE mountain winds, like the dew and rain, sunshine and snow, are
measured and bestowed with love on the forests to develop their strength and beauty.
However restricted the scope of other forest influences, that of the winds is universal.
The snow bends and trims the upper forests every winter...`, 10);
// Example of a customer's profile
const profile = {
isLoggedIn: true,
isPremiumMember: true,
firstTimeUser: false
};
// Examples of evaluating expressions using data from a profile
console.assert( evaluateExpression("isPremiumMember and isLoggedIn", profile) );
console.assert( evaluateExpression("(isPremiumMember and isLoggedIn) or (firstTimeUser)", profile) );
@codyromano
codyromano / simple-react-webpack-setup.bash
Created February 1, 2017 08:26
Run this script to quickly set up a simple template for a React / Webpack project.
#!/bin/bash
# Exit if any command fails
set -e
echo "Choose a project name. Please use only letters, numbers & underscores:"
read projectName
# Clone Pete Hunt's React Webpack template repository
git clone https://github.com/petehunt/react-webpack-template $projectName
@codyromano
codyromano / extractDates.js
Last active January 28, 2017 04:50
Extract dates from a sentence
(function(exports) {
'use strict';
function getFirstDateInteger(sentence = '') {
const dateMatch = /[0-9]{1,2}/.exec(sentence);
return dateMatch && dateMatch[0] || null;
}
function getFirstYearInteger(sentence = '') {
const yearMatch = /[0-9]{4}/.exec(sentence);
/**
* @param {Array} population
* @param {Number} sampleSize
*/
function randomSample(population, sampleSize) {
const populationTotal = population.length;
let result = [];
while (sampleSize--) {
const randIndex = Math.floor(Math.random() * populationTotal);
@codyromano
codyromano / VoiceCommand.js
Created January 17, 2017 07:07
Attach JavaScript callbacks to user voice commands
/**
* @author Cody Romano
* @module VoiceCommand
* @desc Attach JavaScript functions to voice commands
*/
(function(exports) {
'use strict';
var proto = VoiceCommand.prototype;