This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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++) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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++) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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++) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; | |
}; | |
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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++) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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; |