Skip to content

Instantly share code, notes, and snippets.

View jsphkhan's full-sized avatar
🎯
Focusing

Joseph Khan jsphkhan

🎯
Focusing
View GitHub Profile
@jsphkhan
jsphkhan / findarraysum.js
Last active June 23, 2018 16:55
Given an array of numbers, find the 2 elements/numbers from the array that add up to a given sum
/*
eg. [1,2,3,9] Sum = 8, Output = -1 //not found
[1,2,4,4] Sum = 8, Output = 2,3 //found (return the index of matched elements)
Assumption - input array is sorted
This solution here is the fastest way, which is O(n) - linear time
*/
function findElementsThatAddToASum(arr, sum) {
@jsphkhan
jsphkhan / flattenobject.js
Last active June 20, 2018 11:42
Flatten an object using JavaScript
/*
Given an input object
var obj = {
a: {
b: 1,
c: 1,
d: {
e: 1,
f: 1
}
@jsphkhan
jsphkhan / binarysearch.js
Last active June 20, 2018 11:33
Search using JavaScript
//Binary Search
//Runtime - O(log n)
//Faster than Linear search
function binarySearch(arr, num) {
//sort the array
//arr.sort() modifies the original array
arr.sort(function(a, b) {
return a - b;
});
@jsphkhan
jsphkhan / fibonacci.js
Last active June 27, 2018 17:47
Print the fibonacci series till the nth sequence. Use memoization. (JavaScript solution)
//Normal fibonacci - No Memoization
//Fib series - 1 1 2 3 5 8 13 21 34 ...
//O(2^n) = exponential time complexity
function fib(n) {
if(n <= 2) {
return 1;
}
return fib(n-2) + fib(n-1);
}
@jsphkhan
jsphkhan / eventdelegation.js
Created June 12, 2018 18:01
Event Delegation with Plain Vanilla JavaScript
/* Imagine we have a <div id="myDiv"></div> element. Suppose a new element <div class='new-element'></div> is appended
to the parent. Write a JS function that can do event delegation to an element that will be added in future. */
var parentRef = document.getElementById('myDiv'); //document.querySelector('#myDiv')
function delegate(type, elRef, callback) {
parentRef.addEventListener(type, function(evt) {
document.querySelectorAll(elRef).forEach(function(el) {
if(el === evt.target) {
callback.call(null, evt);
}
@jsphkhan
jsphkhan / polymer-iron-list-dynamic-add.html
Created August 18, 2015 13:42
Polymer 1.0 example that shows how to add items to iron-list dynamically using Array mutation techniques
<!doctype html>
<html>
<head>
<title>iron-list dynamic list item</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, minimum-scale=1.0, initial-scale=1, user-scalable=yes">
<meta name="mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-capable" content="yes">
@jsphkhan
jsphkhan / designer.html
Created November 30, 2014 10:34
designer
<link rel="import" href="../yodlee-component/yodlee-scaffold.html">
<link rel="import" href="../yodlee-component/yodlee-navigation.html">
<link rel="import" href="../hackathon-elements/yodlee-account-summary.html">
<link rel="import" href="../hackathon-elements/yodlee-recent-transactions.html">
<link rel="import" href="../hackathon-elements/yodlee-profile-card-bundled.html">
<link rel="import" href="../yodlee-component/yodlee-notifications.html">
<polymer-element name="my-element">
<template>
@jsphkhan
jsphkhan / designer.html
Created November 30, 2014 08:44
designer
<link rel="import" href="../yodlee-component/yodlee-scaffold.html">
<link rel="import" href="../hackathon-elements/yodlee-account-summary.html">
<link rel="import" href="../yodlee-component/yodlee-navigation.html">
<link rel="import" href="../hackathon-elements/yodlee-recent-transactions.html">
<link rel="import" href="../hackathon-elements/yodlee-profile-card-bundled.html">
<link rel="import" href="../yodlee-component/yodlee-notifications.html">
<polymer-element name="my-element">
<template>
@jsphkhan
jsphkhan / designer.html
Created November 30, 2014 08:26
designer
<link rel="import" href="../yodlee-component/yodlee-scaffold.html">
<link rel="import" href="../yodlee-component/yodlee-navigation.html">
<link rel="import" href="../hackathon-elements/yodlee-recent-transactions.html">
<link rel="import" href="../hackathon-elements/yodlee-account-summary.html">
<link rel="import" href="../hackathon-elements/yodlee-profile-card-bundled.html">
<link rel="import" href="../yodlee-component/yodlee-notifications.html">
<polymer-element name="my-element">
<template>
@jsphkhan
jsphkhan / designer.html
Created November 29, 2014 14:52
designer
<link rel="import" href="../yodlee-component/yodlee-scaffold.html">
<link rel="import" href="../yodlee-component/yodlee-navigation.html">
<link rel="import" href="../hackathon-elements/yodlee-account-summary.html">
<link rel="import" href="../hackathon-elements/yodlee-recent-transactions.html">
<link rel="import" href="../yodlee-component/yodlee-notifications.html">
<link rel="import" href="../hackathon-elements/yodlee-profile-card-bundled.html">
<polymer-element name="my-element">
<template>