View reversePolishNotation.js
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 RPN (seq) { | |
if (seq.length <= 2) { | |
console.log('Please enter valid RPN'); | |
return; | |
} | |
let operands = ['+', '-', '*', '/' ], | |
stack = [], | |
i = 0; |
View mergeTwoSortedLists.js
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 mergeTwoSortedLists (l1, l2) { | |
let mergedLinkedListHead = { val : -1, next : null }; // create dummy node to get started | |
let runner = mergedLinkedListHead; | |
while(l1 && l2) { | |
if(l1.val > l2.val) { | |
runner.next = l2; | |
l2 = l2.next; |
View findFirstUniqueCharacter.js
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 findFirstUniqueCharacter( inputString ) { | |
let freqCounter = []; | |
// The issue using Map data structure is during the retrival. As it does not gaurantee the keys will be retrived in the same order as they were inserted | |
// Hence, we use an array of frequency counter. But in this array keys are found using the ascii values of the character. | |
inputString.split('').forEach(ch => { | |
if (!freqCounter[ch]) | |
freqCounter[ch] = 1; |
View Loop-over-an-array-with-timeout.js
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 input = [{ | |
'name': 'event1', | |
'time': 1000 | |
}, { | |
'name': 'event2', | |
'time': 2000 | |
}, { | |
'name': 'event3', | |
'time': 3000 | |
}]; |
View bind.js
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) | |
} | |
} |
View curry2.js
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 add10 (a) { | |
return a + 10 | |
} | |
function compound (f) { | |
return function (b) { | |
return f(f(b)) | |
} | |
} |
View curry1.js
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
let sentence = '' | |
function say () { | |
if ( !arguments[0] ) { | |
let s = sentence | |
sentence = '' | |
return s | |
} else { | |
sentence += arguments[0] + ' ' |
View increamentArray.js
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 increment ( numbers ) { | |
let iterator = numbers.length - 1 | |
while ( iterator >= 0 ) { | |
let num = numbers[ iterator ] | |
num++ | |
if (num > 0 && num <= 9) { |
View findMatchingPair.js
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 twoDiff(arr, target) { | |
let map = {} | |
for (let i = 0; i < arr.length; i++) { | |
let sub = arr[i] - target | |
let add = arr[i] + target | |
if ( map[sub] || map[add] ) { | |
return true |
View customSetInterval.js
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 customSetInterval (cb, interval) { | |
return setTimeout( () => { | |
if (typeof cb == 'function') { | |
cb() | |
// Recurse | |
customSetInterval(cb, interval) | |
} else { | |
console.error(new Error('Expecting a function as a callback')) | |
} |
NewerOlder