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 / javascript_is_weird.js
Created October 19, 2019 12:48
JavaScript can be weird at times
"11" + 1; // "111"
"11" - 1; //10
@jsphkhan
jsphkhan / perfbenchmarkfibonacci.js
Created July 28, 2018 11:24
Perf benchmark for Normal Fibonacci and Fibonacci with Memoization (Dynamic Programming)
//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 July 26, 2018 04:24
Sample Test for Assignment
.html {
background-color: #efefef;
}
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 July 14, 2018 10:32
Sort an array of random 0's and 1's
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 July 14, 2018 10:23
JavaScript Performance Benchmarking
//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 June 30, 2018 17:54
JavaScript Event Debouncing
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 June 27, 2018 18:02
Given an array of numbers, add one to it. Take care of carry overs
/*
[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 / 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 / findfibonaccisequence.js
Created June 27, 2018 17:44
Find the sequence of a given Fibonacci number.
/*
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