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
/** | |
* each() | |
* | |
* Apply an action to every item in an array. | |
*/ | |
const list = [1, 2, 3]; | |
const each = function (array, action) { | |
for (let i = 0; i < array.length; i++) { | |
action(array[i]); |
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
def increment_list_by(list, num): | |
'''Increment all items in a list by `num` | |
provided all items are `int` types. | |
Return modified list if successful, or original list otherwise.''' | |
incremented_list = [] | |
# ============= | |
# Note: below are 2 separate approaches to implement this function | |
# ============= |
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
/* | |
* Complete the function below. | |
* | |
* Sample input 1: ['10 0 0 I love monkeys.'] | |
* Sample output 1 (dashes denote trailing spaces): | |
* ---I love --- | |
* ---monkeys. - | |
* | |
* Sample input 2: ['10 0 0 I love monkeys.', '20 0 0 Good times and bad times.'] | |
* Sample output 2 (dashes denote trailing spaces): |
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
/** | |
* ============================================ | |
* CUSTOM IMPLEMENTATION OF QUEUE CLASS | |
* ============================================ | |
*/ | |
/** | |
* Implementation of Queue. | |
*/ | |
class Queue { |
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
/** | |
* Sort an array in ascending order. | |
* Delete null values before output. | |
* @param {number[]} array | |
* @return {number[]} | |
*/ | |
function bubbleSort (array) { | |
var count = 0 | |
var length = array.length |
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
/** | |
* Binary search tree implementation in JavaScript. | |
* | |
* Note: the BST created here is a BST that is not "balanced," | |
* meaning it has a lot of nodes on one side, | |
* which degrades the BST's O(log n) time complexity. | |
* | |
* Note 2: I'll be adding a balancing function in another snippet. | |
*/ |
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
/** | |
* Binary search in JavaScript. | |
* | |
* For detailed explanation about binary search | |
* read my original blog post: | |
* http://www.nickang.com/binary-search-javascript/ | |
*/ | |
function binarySearch(array, key) { | |
var lower = 0; |
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
/** | |
* Read the original blog post for a detailed explanation! | |
* http://www.nickang.com/linked-list-explained-part-1/ | |
* http://www.nickang.com/linked-list-implementation-part-2/ | |
*/ | |
/** | |
* Constructor functions | |
*/ |