Skip to main content

Posts

Showing posts from August, 2019

Just little basic about recursion dynamic programming and memorization using some simple example in javascript

working sample https://stackblitz.com/edit/js-h3abpu recursion  and memorization these three are the 2 important pillar of computer programming when you are dealing with big task and you want to break your task to smaller parts. let take a example a simple example you have a array and you want to find to maximum value form that array. so let try to solve this problem using recursion //Find a maximum value in a unsorted array //here how i can find max item from a array using recursion function findMax ( arr , len ) { //if length is 1 return first item if ( len == 1 ) { return arr [ 0 ]; } //else recursively search the next maximum item in the array return Math . max ( arr [ len - 1 ], findMax ( arr , len - 1 )) }   now find the minimum item in array function findMin ( arr , len = arr . length ) { if ( len == 1 ) { return arr [ 0 ]; } return Math . min ( arr [ len - 1 ], findMin ( arr , ...
working link  https://stackblitz.com/edit/js-wnzbgw rotate a array (right direction) k times //Right Rotation of a Array -----------------------using O(n) space and O(n) time complexity var myArray =[ 1 , 2 , 3 , 4 , 5 , 6 , 7 ]; function RotationRightFirstMethod ( no_of_times ) { var newArray =[]; var t = myArray . length ; for ( var i = 0 ; i < t ; i ++) { newArray [( i + no_of_times )% t ]= myArray [ i ]; } return newArray ; } console . log ( "result 1" , RotationRightFirstMethod ( 4 )); //lets try using some my own method ---------- var myArray2 =[ 1 , 2 , 3 , 4 , 5 , 6 , 7 ]; function RotationRightSecondMethod ( n ) { var firstArray =[]; var secondArray =[]; for ( var i = 0 ; i < myArray2 . length - n ; i ++) { firstArray . push ( myArray2 [ i ]) } for ( var i = myArray2 . length - n ; i < myArray2 . length ; i ++) { secondArray . push ( myArray2 [ i ]); } return secondArray . c...

polyfill for map and filter method in javascript

working link==> https://stackblitz.com/edit/js-wpw67a //check out filter map and reduce once //create your own map function or polyfill for map //map callback accept three parameter one value second index and third one is a current array //create a polyfill for map function Array . prototype . myMap = function ( callBack ) { if ( typeof callBack != "function" ) { throw new TypeError (); } var newArray =[]; for ( var i = 0 ; i < this . length ; i ++) { //this is current Array //i is index //this[i] is current item newArray . push ( callBack . apply ( null ,[ this [ i ], i , this ])); } return newArray ; } var myArray =[ 1 , 2 , 3 , 4 , 5 ]; var newArray = myArray . myMap (( value , index )=>{ return value * index ; }) console . log ( newArray ); //now polyfill for filter which accept three parameter one value second index and...

function composition (function programming) over inheritance javascript with example

working link https://stackblitz.com/edit/js-d57tvk more about https://www.youtube.com/results?search_query=functional+programming+vs+object+oriented+javascript a better link https://www.youtube.com/watch?v=7HolHe7Gqbw&t=263s example :- //function composition (function programming) over inheritance //create some global function every thing except number and string are object in javascript //@function showName---------------------------- const showName =( state )=>({ getfullName () { return state . name + " " + state . last ; } }); //@function showSalary---------------------------- const showSalary =( state )=>( { bonus : 800 , getSal () { return state . sal * 12 + this . bonus ; } } ) //@function what emp do const WhatHeDo =( state )=>( { getType () { switch ( state . type ) { case "it" : ...

creating a dynamic tabbar in reactjs

working link https://stackblitz.com/edit/react-scyhqh import React , { Component } from 'react' ; import { render } from 'react-dom' ; import Hello from './Hello' ; import './style.css' ; function TabButton ( props ) { return < button className = "button" onClick ={()=>{ if ( props . onClick != null ) { props . onClick ({ label : props . label }); } }}>{ props . label }</ button > } class App extends Component { constructor () { super (); this . currentPage = 0 ; this . totalPage = 4 ; this . buttonData =[ { label : "manish1" }, { label : "sachin2" }, { label : "deepak3" }, { label : "sanjay4" }, { label : "manish21" }, { label : "sachin22" }, { label : "de...

checking intersection between two array in javascript

working link https://stackblitz.com/edit/js-6e4q5n /** * @param {number[]} nums1 * @param {number[]} nums2 * @return {number[]} */ var intersect = function ( nums1 , nums2 ) { var dic ={}; var arr =[]; var count = 0 ; if ( nums2 . length >= nums1 . length ) { var temp = nums1 ; nums1 = nums2 ; nums2 = temp ; } for ( var i = 0 ; i < nums1 . length ; i ++) { dic [ nums1 [ i ]]= 1 ; } console . log ( dic ); for ( var i = 0 ; i < nums2 . length ; i ++) { if ( dic [ nums2 [ i ]]) { arr . push ( nums2 [ i ]); dic [ nums2 [ i ]]= 0 ; } } return arr ; }; console . log ( intersect ([ 3 , 2 , 1 ],[ 1 , 1 ] ));

creating a custom function composition is javascript to understand the concept of function composition in javascript

working link is here https://stackblitz.com/edit/js-ilkdqf //here is a small ployfill of bind method..................... var myObject = { name : "manish" , age : 22 } function showName ( engg , sal ) { console . log ( "my name is " + this . name + " and my age is" + " " + this . age + " " + "and i work as a engg " + engg + " " + "and my salary is " + sal ); } const a = showName . bind ( myObject , "software" , 20000 ); a (); console . log ( "mybind starts from here...............>>>>" ) Function . prototype . myBind = function () { var thisFunction = this ; var obj = arguments [ 0 ]; if ( typeof obj != "object" ) { throw new Error ( "bind method required a object" ); } var args =[]; for ( var i = 1 ; i < arguments . length ; i ++) { ar...

how to create a Polyfill of popular bind method in javascript

working link=> https://stackblitz.com/edit/js-nntys1 //here is a small ployfill of bind method..................... var myObject = { name : "manish" , age : 22 } function showName ( engg , sal ) { console . log ( "my name is " + this . name + " and my age is" + " " + this . age + " " + "and i work as a engg " + engg + " " + "and my salary is " + sal ); } const a = showName . bind ( myObject , "software" , 20000 ); a (); console . log ( "mybind starts from here...............>>>>" ) Function . prototype . myBind = function () { var thisFunction = this ; var obj = arguments [ 0 ]; if ( typeof obj != "object" ) { throw new Error ( "bind method required a object" ); } var args =[]; for ( var i = 1 ; i < arguments . length ; i ++) { args . ...