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
function quicksort(array) { | |
if (array.length <= 1) { | |
return array; | |
} | |
var pivot = array[0]; | |
var left = []; | |
var right = []; |
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
// Split the array into halves and merge them recursively | |
function mergeSort (arr) { | |
if (arr.length === 1) { | |
// return once we hit an array with a single item | |
return arr | |
} | |
const middle = Math.floor(arr.length / 2) // get the middle item of the array rounded down | |
const left = arr.slice(0, middle) // items on the left side | |
const right = arr.slice(middle) // items on the right side |
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
const insertionSort = a => { | |
var n = a.length; | |
for(var i=1;i<n;i++){ | |
var key = a[i]; | |
var j = i-1; | |
while(j>=0 && a[j]>key){ | |
a[j+1] = a[j]; | |
j=j-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
function cloneObject(obj) { | |
var clone = {}; | |
for(var i in obj) { | |
if(typeof(obj[i])=="object" && obj[i] != null) | |
clone[i] = cloneObject(obj[i]); | |
else | |
clone[i] = obj[i]; | |
} | |
return clone; | |
} |
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
// Time complexity: O(n^2) | |
function findTriplets(arr, n) { | |
arr.sort(); | |
var l = arr.length; | |
for (var i = 0; i < l; i++) { | |
var j = i + 1, | |
k = l - 1; | |
while (j < k) { | |
if (arr[i] + arr[j] + arr[k] < n) { | |
j++; |
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
// a simple pure function to get a value adding 10 | |
const add = (n) => (n + 10); | |
console.log('Simple call', add(3)); | |
// a simple memoize function that takes in a function | |
// and returns a memoized function | |
const memoize = (fn) => { | |
let cache = {}; | |
return (...args) => { | |
let n = args[0]; // just taking one argument here | |
if (n in cache) { |
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
// Shape - superclass | |
function Shape() { | |
this.x = 0; | |
this.y = 0; | |
} | |
// superclass method | |
Shape.prototype.move = function(x, y) { | |
this.x += x; | |
this.y += 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
Function.prototype.bind1 = function (scope) { | |
let fn = this | |
let prefixArgs = Array.prototype.slice.call(arguments, 1) | |
return function() { | |
let suffixArgs = Array.prototype.slice.call(arguments) | |
let args = prefixArgs.concat(suffixArgs) | |
return fn.apply(scope, args) | |
} | |
} |
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
Function.prototype.myApply = function(){ | |
if(arguments[0]==null||arguments[0]==this){ | |
return this.bind(...arguments[1])(); | |
} | |
else{ | |
return this.bind(...arguments[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
Function.prototype.myCall = function(){ | |
return this.bind(...arguments)(); | |
} | |
ex- | |
function showProfileMessage(message) { | |
console.log(message, this.name); | |
} | |
const obj = { | |
name: "Ankur Anand" |
NewerOlder