Skip to content

Instantly share code, notes, and snippets.

@alexhawkins
alexhawkins / underscoreIMPS.js
Last active August 29, 2015 14:05
Underscore Method Implementations
//#######PLUCK#######
var myPluck = function(obj, propName) {
var arr = [];
for (var i = 0; i < obj.length; i++) {
for (var prop in obj[i]) {
if (prop === propName)
arr.push(obj[i][prop]);
}
}
@alexhawkins
alexhawkins / uniq.js
Created August 15, 2014 00:00
A Unique Sorting Function in JS.
arr = [7, 7, 9, 2, 45, 6, 34, 43, 2, 6, 8, 2, 4, 100, 100, 100, 23, 34, 4];
var uniq = function(arr) {
return arr.filter(function(value, index, self) {
// only returns uniq numbers. A duplicate will yield a different indexOf
return self.indexOf(value) === index;
}).sort(function(x, y) {
return x - y;
});
};
@alexhawkins
alexhawkins / shuffle.js
Created August 21, 2014 02:28
Shuffle a Deck of Cards
var shuffle = function(arr) {
var rand, temp;
//loop through array and swap out value at random position
//with value at iteration position of array
for (var i = 0; i < arr.length; i++) {
//generate random number between 0 and length of array.
rand = Math.floor(Math.random() * arr.length);
temp = arr[i]; //store value of arr[i] to swap later
arr[i] = arr[rand]; //set arr[i] to random position value
arr[rand] = temp; //replace with temp
@alexhawkins
alexhawkins / CaesarCipher.js
Created August 21, 2014 23:37
Basic Caesar Cipher and Decipher (send ciphered e-mails and texts with this simple code)
var cipher = function(str, num) {
var code = 0,
cipher = '';
for (var i = 0; i < str.length; i++) {
code = str.charCodeAt(i);
//check lowercase
if (code >= 97 && code <= 122) {
code += num;
@alexhawkins
alexhawkins / recursionBasics.js
Last active August 29, 2015 14:06
Recursion Basics: How to Solve Problems Recursively
/* RECURSION ( when a function invokes itself to solve a problem )
All recursive functions have the following characteristics:
1) The method is implemented using if-else logic that leads to
different cases.
2) One or more base/terminating cases(the simplest case) are
used to stop the recursion ex. if (n === 0) stop
3) Every recursive call reduces the original problem, bringing
it increasingly closer to the terminating base case until
@alexhawkins
alexhawkins / JSON.stringify.js
Last active August 29, 2015 14:06
Hardly Readable JSON.stringify Implementation
var stringifyJSON = function(obj) {
var objElements = [];
//check for literals
if (!(obj instanceof Object))
return typeof obj === 'string' ? '"' + obj + '"' : '' + obj;
//check for arrays
else if (Array.isArray(obj)) {
return '[' + obj.map(function(el) { return stringifyJSON(el); }) + ']';
//check for object if not array
} else if (obj instanceof Object) {
@alexhawkins
alexhawkins / bubbleSort.js
Last active August 29, 2015 14:06
Various Bubble/Selection/Quick/Merge Sort Implementations
/* BASIC BUBBLE SORT ALGORITHM
1. Compare first item to the second item
2. Swap if first item should be after second
3. Compare second to third item
4. If second should be after third, swap
5. Continue until end of data set;
*/
//FAST BUBBLE SORT
@alexhawkins
alexhawkins / stackNqueues.js
Last active August 29, 2015 14:08
Functional, Functional-Shared, Prototypal, Pseudoclassical implementations of Stacks and Queues in JavaScript
/******************************************************/
/******************************************************/
/**
* FUNCTIONAL QUEUE
*/
/** Queues are FIFO, first in first out */
'use strict';
var makeQueue = function() {
@alexhawkins
alexhawkins / lastSurvivor.js
Last active August 29, 2015 14:14
Last Survivor Coding Question
'use strict';
/** APPLICANT INFO ***********************************************************
------------------------------------------------------------------------------
* Name: Alex Hawkins
* Email: alexhawkins.me@gmail.com
* GitHub: github.com/alexhawkins
* LinkedIn: linkedin.com/in/alexhawkinsme/
****************************************************************************/
@alexhawkins
alexhawkins / codeChallenges.js
Last active August 29, 2015 14:15
Code Challengees
//1) reverse a string without using any variables or
/*****************************************************/
function reverseString(s) {
if (s === '')
return '';
else
return s.slice(1) + s[0];
}