Skip to content

Instantly share code, notes, and snippets.

View reversePolishNotation.js
function RPN (seq) {
if (seq.length <= 2) {
console.log('Please enter valid RPN');
return;
}
let operands = ['+', '-', '*', '/' ],
stack = [],
i = 0;
View mergeTwoSortedLists.js
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
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
var input = [{
'name': 'event1',
'time': 1000
}, {
'name': 'event2',
'time': 2000
}, {
'name': 'event3',
'time': 3000
}];
View bind.js
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
function add10 (a) {
return a + 10
}
function compound (f) {
return function (b) {
return f(f(b))
}
}
View curry1.js
let sentence = ''
function say () {
if ( !arguments[0] ) {
let s = sentence
sentence = ''
return s
} else {
sentence += arguments[0] + ' '
View increamentArray.js
function increment ( numbers ) {
let iterator = numbers.length - 1
while ( iterator >= 0 ) {
let num = numbers[ iterator ]
num++
if (num > 0 && num <= 9) {
View findMatchingPair.js
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
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'))
}