Skip to content

Instantly share code, notes, and snippets.

Avatar
🎯
Focusing

Joseph Khan jsphkhan

🎯
Focusing
View GitHub Profile
@jsphkhan
jsphkhan / javascript_is_weird.js
Created Oct 19, 2019
JavaScript can be weird at times
View javascript_is_weird.js
"11" + 1; // "111"
"11" - 1; //10
@jsphkhan
jsphkhan / perfbenchmarkfibonacci.js
Created Jul 28, 2018
Perf benchmark for Normal Fibonacci and Fibonacci with Memoization (Dynamic Programming)
View perfbenchmarkfibonacci.js
//Fibonacci series perf comparisions
//Recursive Call vs Dynamic Programing
//1 1 2 3 5 8 13 21
//normal recursion - O(2^n)
function fibonacci1(seq) {
if(seq === 1 || seq === 2) {
return 1;
}
@jsphkhan
jsphkhan / file.css
Last active Jul 26, 2018
Sample Test for Assignment
View file.css
.html {
background-color: #efefef;
}
View fetch_iterator_promise.js
fetchSynchronous(fetchFactories) {
// we have to fetch the data sequentially in order to avoid overloading the server
// this array will hold the data from each response
const data = [];
// init a promise chain
let p = Promise.resolve();
// execute promises in a chain
fetchFactories.forEach((fetchFactory) => {
@jsphkhan
jsphkhan / sortzeroandone.js
Created Jul 14, 2018
Sort an array of random 0's and 1's
View sortzeroandone.js
var arr = [0,1,1,0,0,0,1,1,1,1]; //given array
//Outut = [0,0,0,0,1,1,1,1,1,1]
var len = arr.length; //find the length of the array
//count the no of zeros in the array
var count = 0;
for(var i=0;i<len;i++) {
if(arr[i] === 0) {
count++;
}
}
@jsphkhan
jsphkhan / jsperfbenchamrk.js
Last active Jul 14, 2018
JavaScript Performance Benchmarking
View jsperfbenchamrk.js
//Perf benchmarking for array sorting.
//Run these lines in the browser console
//1. Bubble Sort
var t1 = performance.now();
var arr = [29, 30, 100, 300, 400, 12, 1, 45, 67, 29, 30, 100, 300, 400, 12, 1, 45, 67, 29, 30, 100, 300, 400, 12, 1, 45, 67];
for(var i=0;i<arr.length;i++) {
for(var j=0; j<arr.length-i-1;j++) {
if(arr[j] > arr[j+1]) {
@jsphkhan
jsphkhan / debounce.js
Created Jun 30, 2018
JavaScript Event Debouncing
View debounce.js
var ref = document.querySelector('#myDiv');
function foo() {
console.log('hello');
}
function debounce(callback, time) {
var timer = null;
return function() {
clearTimeout(timer);
@jsphkhan
jsphkhan / arrayaddone.js
Created Jun 27, 2018
Given an array of numbers, add one to it. Take care of carry overs
View arrayaddone.js
/*
[1,2,3,4] => [1,2,3,5]
[1,2,9] => [1,3,0]
[1,2,9,9] => [1,3,0,0]
[9,9,9] => [1,0,0,0]
*/
function addOne(arr) {
let sum = 0, carry = 1;
for(let i = (arr.length - 1); i >= 0; i--) {
sum = arr[i] + carry;
@jsphkhan
jsphkhan / findfibonaccisequence.js
Created Jun 27, 2018
Find the sequence of a given Fibonacci number.
View findfibonaccisequence.js
/*
Say you are given a number from Fibonacci series - 13, find its sequence number
Input - 13, Output - 7
Input - 21, Output - 8
*/
function findFibSeq(num) {
var a = 1, b = 1, sum = 0, n = 2;
//continue to run until sum and provided number is not equal
@jsphkhan
jsphkhan / objectcopy.js
Last active Jun 23, 2018
Shallow and Deep Copy of objects
View objectcopy.js
/*
In JS, objects, arrays and functions are passed by reference. Rest all are copied for eg. primitive values.
*/
//Shallow Copy
/*
var obj = {a: 1, b: [1,2,3], c: function() {console.log('test func')}};
newObj = {a: 1, b: [1,2,3], c: function() {console.log('test func')}};
Problem with shallow copy is that the array/function inside newObj is copiedd by reference. So newObj.b.push(5),
You can’t perform that action at this time.