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
'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); | |
} | |
}; |
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
'use strict'; | |
/*****************NATIVE forEACH*********************/ | |
Array.prototype.myEach = function(callback) { | |
for (var i = 0; i < this.length; i++) | |
callback(this[i], i, this); | |
}; | |
//tests |
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
/* SIMPLIFIED UNDERSCORE .EACH method*/ | |
var _ = function(obj) { | |
if (obj instanceof _) return obj; | |
if (!(this instanceof _)) return new _(obj); | |
this._wrapped = obj; | |
}; | |
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
//#######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]); | |
} | |
} |
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
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; | |
}); | |
}; |
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 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 |
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 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; |
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
//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) |
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 _ = {}; | |
/*********IDENTITY**********/ | |
_.identity = function(val) { | |
return val; | |
}; | |
/*********FIRST**********/ | |
_.first = function(array, n) { |
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
/* 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 |