Skip to content

Instantly share code, notes, and snippets.

View mrandrewmills's full-sized avatar

Andrew Mills mrandrewmills

View GitHub Profile
function bionicizeText(str) {
let phrase = str.split(" ");
let results = [];
results.push(phrase.map(word => bionicizeWord(word)));
return results.join(" ");
function bionicizeWord(w) {
let strong = document.createElement('strong');
@mrandrewmills
mrandrewmills / isPrime.js
Created December 16, 2023 22:15
isPrime - function to test whether given number is a prime
function isPrime(thisNum){
if ( thisNum <= 1 )
return false;
for ( x = 2; x <= Math.sqrt(thisNum); x++ ){
if ( thisNum % x == 0 )
return false;
}
return true;
@mrandrewmills
mrandrewmills / multiSortArrayStruct.js
Last active July 4, 2022 02:22
JS to sort an array of structures by one or more properties
function propComparator(sortBy){
return function(a,b){
let fieldsToSortBy = sortBy.split(",");
for ( x = 0; x < fieldsToSortBy.length; x++ ) {
let results = 0;
@mrandrewmills
mrandrewmills / knapsackSolved.js
Last active August 9, 2021 00:40
Solving The Knapsack Problem
// inspired by Academind's "JavaScript Algorithms Crash Course"
// see https://www.youtube.com/watch?v=JgWm6sQwS_I for more like this
// Imagine you have a list of items, and each item has a value and a weight associated with it.
items = [
{ id: "b", val: 6, w: 8 },
{ id: "a", val: 10, w: 3 },
{ id: "c", val: 3, w: 3 },
];
@mrandrewmills
mrandrewmills / titleAnnoyance.js
Created July 5, 2021 15:51
JS object for toggling document.title text between current value and a temporary one at a specified interval. Also a method to stop/clear.
function titleToggle( currTitle, tmpTitle, msInterval){
var currTitle = currTitle;
var tmpTitle = tmpTitle;
var msInterval = msInterval;
var timerID = -1;
this.stop = function() {
if ( timerID != -1) {
clearInterval(timerID);
@mrandrewmills
mrandrewmills / LuckyNumberGameTrials.js
Created September 16, 2020 09:06
quick and dirty JS to see if low to high, high to low, or random approach is best for lucky number games
// gets a random number between min and max, inclusive. Source: Mozilla Developer Network
function getRandomIntInclusive(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min + 1) + min);
}
// find lucky number, going from 1 to 100, in sequence
function runLowToHighTrial(){
@mrandrewmills
mrandrewmills / inspectAllTables.cfm
Created September 17, 2019 12:09
When you don't have DB access, but need to see the tables & field names with which you are working.
<!--- change datasource as needed --->
<cfset thisDatasource = "example">
<!--- retrieve and display all table names in order --->
<cfquery name="getAllTableNames" datasource="#thisDatasource#">
SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE='BASE TABLE'
ORDER BY Table_Name
</cfquery>
<cfdump var="#getAllTableNames#" label="getAllTableNames" expand="false">
@mrandrewmills
mrandrewmills / waybackThis.js
Created July 15, 2019 12:55
JS Bookmarklet which shows the current page in the Internet Archive's Wayback machine.
javascript: (function () { /* finds current URL in Wayback Machine */ document.location = "https://web.archive.org/web/*/" + document.location; }());
@mrandrewmills
mrandrewmills / ssnRegExp.js
Created July 6, 2019 18:30
JS RegExp for validating Social Security Numbers
var ssnRegExp = new RegExp(/^(?!000|666)[0-8][0-9]{2}-(?!00)[0-9]{2}-(?!0000)[0-9]{4}$/);
// see p. 290 Regular Expressions Cookbook, 2nd Ed., Goyvaerts & Levithan ISBN: 978-1-449-31943-4 for in-depth details
ssnRegExp.test("000-11-1111");
// returns false
ssnRegExp.test("111-11-1111");
// returns true
ssnRegExp.test("666-11-1111");
// returns false
@mrandrewmills
mrandrewmills / arrangeArrayStructs.js
Created June 29, 2019 20:27
Ever needed to arrange an Array of Structs in a specific order which wasn't alphabetical or numerical? (e.g. Federal Holidays, or Beatles) This function can help you with that.
function arrangeArrayStructs(arrayToArrange, arrOrder, whichKey){
var results = [];
arrOrder.forEach( function(item){
results.push( arrayToArrange.filter( record => record[whichKey] == item ) );
});
return results;
}