Skip to content

Instantly share code, notes, and snippets.

@Daniel-Hug
Daniel-Hug / index-of-min.js
Last active June 6, 2017 20:17
JS function: Get the index of the first smallest value in an array
// Get the index of the first smallest value in an array
function indexOfMin(arr) {
if (arr.length === 0) {
return -1;
}
var min = arr[0];
var minIndex = 0;
for (var i = 1; i < arr.length; i++) {
@Daniel-Hug
Daniel-Hug / index-of-max.js
Created June 6, 2017 20:14
JS function: Get the index of first of the largest values in an array
// Get the index of first of the largest values in an array
function indexOfMax(arr) {
if (arr.length === 0) {
return -1;
}
var max = arr[0];
var maxIndex = 0;
for (var i = 1; i < arr.length; i++) {
@Daniel-Hug
Daniel-Hug / prefix-function.js
Last active June 6, 2017 20:04
JS functions: get the border lengths of every prefix or suffix in a string
function getPrefixBorders(string) {
// This will contain the border length for each
// prefix in ascending order by prefix length.
var borderLengthByPrefix = [0];
// This is the length of the border on the current prefix.
var curBorderLength = 0;
// Loop from the 2nd character to the last.
for (var i = 1; i < string.length; i++) {
function getSuffixBorders(string) {
// This will contain the border length for each
// suffix in ascending order by suffix length.
var borderLengthBySuffix = [0];
// This is the length of the border on the current suffix.
var curBorderLength = 0;
// Loop from the 2nd to last character to the first.
var lastI = string.length - 1;
@Daniel-Hug
Daniel-Hug / map-to-white-keys.js
Created June 2, 2017 20:06
Map a number 0-51 to the corresponding white key number on a piano
var mapToWhiteKeys = (function() {
var whites = [1, 3, 4, 6, 8, 9, 11, 13, 15, 16, 18, 20, 21, 23, 25, 27, 28, 30, 32, 33, 35, 37, 39, 40, 42, 44, 45, 47, 49, 51, 52, 54, 56, 57, 59, 61, 63, 64, 66, 68, 69, 71, 73, 75, 76, 78, 80, 81, 83, 85, 87, 88];
return function mapToWhiteKeys(indexOfWhite) {
return whites[indexOfWhite];
};
})();
// given an array of numbers scale each value so that
// the smallest is newMin and the largest is newMax
// while maintaining the ratio of distances between values
function stretchRange(nums, newMin, newMax) {
var min = Math.min.apply(Math, nums);
var max = Math.max.apply(Math, nums);
var range = max - min;
var newRange = newMax - newMin;
var scaled = [];
for (var i = 0; i < nums.length; i++) {
@Daniel-Hug
Daniel-Hug / index-of-single-min.js
Last active June 6, 2017 20:12
JS function: Return the index of the *single* smallest value in array
// Returns the index of the smallest value in arr if it
// is the *single* smallest value, otherwise returns -1.
function indexOfSingleMin(arr) {
if (arr.length === 0) {
return -1;
}
var min = arr[0];
var minIndex = 0;
var notAlone = false;
// indexOfBest gos through arr, to find the index of the single "best" item if there is one.
// It compares each item with the current best by passing both to a compare function.
//
// The compare function should return a number > 0 if the first argument is better than
// the second, a number < 0 if the first is worse, or 0 if they're of equal quality.
function indexOfBest(arr, compare) {
if (arr.length === 0) {
return -1;
}
// Returns an array of the number of ways to choose
// k items from a set of n items going from k = 0...n
// unsafe for n >= 52
function binomialCoefficients(n) {
var coefficients = [];
coefficients[0] = 1;
for (var k = 0; k < n; k++) {
coefficients[k+1] = (coefficients[k] * (n-k)) / (k+1) ;
}
return coefficients;
// Generate random integer within range:
// var die = getRandomInt(1,6);
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
function sumArray(array) {
var sum = 0;
for (var i = 0, l = array.length; i < l; i++) sum += array[i];
return sum;