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
"11" + 1; // "111" | |
"11" - 1; //10 |
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
//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; | |
} |
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
.html { | |
background-color: #efefef; | |
} |
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
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) => { |
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 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++; | |
} | |
} |
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
//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]) { |
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 ref = document.querySelector('#myDiv'); | |
function foo() { | |
console.log('hello'); | |
} | |
function debounce(callback, time) { | |
var timer = null; | |
return function() { | |
clearTimeout(timer); |
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
/* | |
[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; |
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
//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); | |
} |
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
/* | |
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 |
NewerOlder