Skip to content

Instantly share code, notes, and snippets.

View mrandrewmills's full-sized avatar

Andrew Mills mrandrewmills

View GitHub Profile
@mrandrewmills
mrandrewmills / JavaScript Puzzle Two (jQuery)
Created January 10, 2017 22:40
Challenge: Explain the cause of the bug, and fix it by changing only a single line.
// 1st given: the .get call on line 5 should fire only when .sidebar is present
// 2nd given: the current page does NOT have any element with a .sidebar attribute
if ($('.sidebar')) {
$.get( "ajax/fundraising.html", function( data ) {
$( '.sidebar' ).html( data );
});
}
@mrandrewmills
mrandrewmills / Bookmarklet for CommonSpot PageID
Created January 19, 2017 01:12
JavaScript bookmarklet which displays CommonSpot pageID value in console
javascript: (function () {/*retrieves pageID of CommonSpot pages quickly*/ if(typeof jsPageID!=="undefined"){console.log("Page ID is: " + jsPageID);}else{console.log("No Page ID found here.")}}());
@mrandrewmills
mrandrewmills / comicPanels.js
Last active April 1, 2017 03:18
quick and dirty JavaScript to help build comic panel grid in a hurry
function comicPanels(rows, columns, width, height, border, spacing, padding) {
// our properties
this.rows = rows || 3;
this.columns = columns || 2;
this.width = width || 800;
this.height = height || 600;
this.border = border || 1;
this.spacing = spacing || 0;
this.padding = padding || 0;
@mrandrewmills
mrandrewmills / PalindromeChallenge.js
Last active August 13, 2017 12:08
A function to test if a given phrase is or is not a palindrome (i.e. the same forwards as it is backwards).
function isPalindrome(phrase) {
"use strict";
var letters, x;
// turn our phrase into only ALPHA CAPS
phrase = phrase.match(/[a-zA-Z]/g);
phrase = phrase.toString();
phrase = phrase.replace(/[,]/g, "");
@mrandrewmills
mrandrewmills / palindrome.js
Created August 7, 2017 12:53
a "battle-tested" revision of the isPalindrome function, supports numerics.
function isPalindrome(phrase) {
"use strict";
var forwards, backwards;
forwards = phrase
.toUpperCase() // convert to upper case
.match(/[A-Z0-9]/g) // an array of only alphanumerics
.toString() // convert back to CSV string
@mrandrewmills
mrandrewmills / isPalindrome.js
Last active August 24, 2017 02:17
the many ways to "skin" a palindrome . . .
/**
* isPalindrome - tests phrase for alphanumeric symmetry
* @param {String} phrase to be tested
* @returns {Bool} true if a palindrome, false if not
*/
class MyString extends String {
isPalindrome() {
"use strict";
@mrandrewmills
mrandrewmills / findDuplicateIDs.js
Created September 1, 2017 11:52
find duplicate values in array of structures for a unique id field
/**
* findDuplicateIDs - scan array of structs for duplicate values in unique id field
* @param {Array} - the array to be scanned
* @param {String} - name of unique id field in array
* @return {Object} - object with lists of unique and duplicated ids
*
* Examples:
* findDuplicateIDs( alerts, "unique_id" );
* findDuplicateIDs( people, "SSN" );
*/
@mrandrewmills
mrandrewmills / deDuplicate.js
Created September 2, 2017 18:31
remove duplicates from Array of Objects/Structures, based on unique value in specified property.
/**
* deDuplicate - remove duplicates from Array of Objects/Structures by value of a Unique ID
* @param {Array} - the Array of Objects/Structures
* @param {String} - the field to use as a Unique ID
* @returns {Array} - an Array of Objects/Structures, filtered by the Unique ID property specified
* Example:
* deDuplicate( people, "SSN" ); // filter people array by unique values in people[x].SSN property
*/
function deDuplicate(myArray, myUniqueID) {
"use strict";
@mrandrewmills
mrandrewmills / wordFreq.js
Created September 4, 2017 21:14
JS function for word frequency analysis
function wordFreq(myText) {
"use strict";
var results = [];
myText.split(" ").forEach(function(element) {
var foundIt = results.findIndex( x => x.word === element);
@mrandrewmills
mrandrewmills / charFreq.js
Created September 5, 2017 11:25
JS function for character frequency analysis
function charFreq(myText) {
"use strict";
var results = [];
myText.split("").forEach(function(element) {
var foundIt = results.findIndex( x => x.char === element);