Skip to content

Instantly share code, notes, and snippets.

@alexhawkins
alexhawkins / recursionEx.js
Last active February 21, 2020 23:53
Hack Reactor Sample Recursion Exercises
'use strict';
//######### EXAMPLE 1 #################/
// Implement function countDecrementing that takes a number `n` and logs numbers n to 0
var countDown = function(num) {
if (num >= 0) {
console.log(num);
countDown(num - 1);
}
};
@alexhawkins
alexhawkins / nativeJavaScript.js
Last active April 28, 2024 08:52
Implementation of Native JavaScript Methods (forEach, Map, Filter, Reduce, Every, Some)
'use strict';
/*****************NATIVE forEACH*********************/
Array.prototype.myEach = function(callback) {
for (var i = 0; i < this.length; i++)
callback(this[i], i, this);
};
//tests
@alexhawkins
alexhawkins / underscoreRemix.js
Last active August 29, 2015 14:05
Simplified Underscore .Each method
/* SIMPLIFIED UNDERSCORE .EACH method*/
var _ = function(obj) {
if (obj instanceof _) return obj;
if (!(this instanceof _)) return new _(obj);
this._wrapped = obj;
};
@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 / stringifyJSON.js
Created September 16, 2014 03:54
Recursive JSON.stringify implementation
//Recursive implementation of jSON.stringify;
var stringifyJSON = function(obj) {
var arrOfKeyVals = [],
arrVals = [],
objKeys = [];
/*********CHECK FOR PRIMITIVE TYPES**********/
if (typeof obj === 'number' || typeof obj === 'boolean' || obj === null)
@alexhawkins
alexhawkins / AllUnderscoreMethods.js
Last active January 12, 2023 05:12
Basic Implementation of 'most' Underscore Methods from Scratch
var _ = {};
/*********IDENTITY**********/
_.identity = function(val) {
return val;
};
/*********FIRST**********/
_.first = function(array, n) {
@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