Posts

Showing posts from June, 2023

Flatten an array Completely

 const arr = [1, 2, 3, [4, 5, 6], [7, 8, [9, 10, 11], 12], [13, 14, 15]]   var flat = function (arr, n) {     // return arr.flat(n)     let modifiedArr=arr;     for(let i =0 ;i <n ;i++){     let output=modifiedArr.reduce((acc,curr)=>{         if(Array.isArray(curr)){             return [...acc,...curr];         }         else{           return [...acc,curr]         }              return acc;         },[])     modifiedArr=output;     } return modifiedArr; }; console.log(flat(arr,0)) var flat = function(arr) {   let modifiedArr = [];   arr.forEach((item) => {     if (Array.isArray(item)) {       modifiedArr.push(...flat(item)); // Recursively flatten nested arrays     } else { ...

Piping Function

Image
 What is a piping function ? The result of one function will be the argument of another function. Normal code ---> const val ={ salary:10000 } const getSalary =(person)=> person.salary; const addBonus = (netSalary)=>netSalary+1000; const deductTax=(grossSalary) =>{ const percentDeduction =grossSalary * 0.3 return grossSalary - percentDeduction } const pipe = (getSalary,addBonus,deductTax)=>{ return (val)=>{ const person=val; const netSalary=getSalary(person); const grossSalary =addBonus(netSalary); const CreditSalary = deductTax(grossSalary); return CreditSalary; } } const result =pipe(getSalary,addBonus,deductTax)(val); console.log(result) PIPING CODE const val ={ salary:10000 } const getSalary =(person)=> person.salary; const addBonus = (netSalary)=>netSalary+1000; const deductTax=(grossSalary) =>grossSalary - (grossSalary * 0.3) const pipe = (...fns)=>{ return (val)=>{ fns.for...

Cube

 //caching //addition , a+b+c  add(2,3) // sum operation is a heavy operator  const sortedString=(arr)=>{   const sort = arr.sort((a,b)=>a-b)   return sort.join('').toString() } let cache ={}; const add = (...allVal)=>{   //look for the value exist or not   if(cache[sortedString(allVal)]){       console.log('from cache')     return cache[sortedString(allVal)]   }   else{     const sum = allVal.reduce((acc,curr)=>{     acc+=curr;     return acc;   },0)   cache[sortedString(allVal)]=sum;   console.log('from computation')   return sum;   } } console.log(add(2,3,4,5)) console.log(add(2,3,4,5)) // let cache={ //   '2':5,  //   '32':5 // } // console.log(cache['23']|| cache['32']) FIXED LOGIC const sortedString = (arr) => {   return arr.sort((a, b) => a - b) .join(','); }; let cache = {}; const add = (...allVal) => { ...

File Explorer

https://file-explorer-project.vercel.app/ https://github.com/ankittyagi206/explorer

Interview Questions

 // // Input- abbcccdddd // // Output- a1b2c3d4 // const input= "abbcccdddd".split('').reduce((acc,curr,index,arr)=>{ //   co //   if(acc[curr]){ //     acc[curr]+=1; //   } //   else{ //     acc[curr]=1; //   } //   return acc;    // },{}) // console.log(input) // Sum(1)(2)(3)(4)() // Output-10 // const Sum = function (val){ //   return (val1)=>{ //     return (val2)=>{ //       return (val3)=>{ //         return val+val1+val2+val3 //       } //     }   //   } // } const Sum = function (val) {   return function (val1) {     return function (val2) {       return function (val3) {         if (val1 !== undefined && val2 !== undefined && val3 !== undefined) {         ...

Interview Machine Code Round: File Explorer

  https://www.youtube.com/watch?v=20F_KzHPpvI&t=119s const arr =[0,3,0,10,0,2] //3,10,2,0,0,0 const ans=[]; for( let i=0; i< arr.length ;i++){ if(arr[i]!==0){ ans.push(arr[i]) } } if(ans.length !==arr.length){ let getremanining= arr.length -ans.length; for(let i=0;i <getremanining ;i++){ ans.push(0); } } console.log(ans)

Hoisting In Javascript

Image
https://www.youtube.com/watch?v=Fnlnw8uY6jo Hoisting --> var x= 7; function getName(){ console.log("Namaste Javascript") } getName(); console.log(x); output-->  Namaste Javascript 7 But what if we try to access them before the initialization.  getName(); console.log(x); var x= 7; function getName(){ console.log("Namaste Javascript") } Output: Namaste Javascript undefined means getName was able to find the getName and invoked the function directly but var x was not. what is this undefined?? what if I even remove the initialization of x?? getName(); console.log(x); function getName(){ console.log("Namaste Javascript") } Output: Namaste Javascript index.js:3 console.log(x); ^ ReferenceError: x is not defined at Object.<anonymous> (index.js:3:13) at Module._compile (node:internal/modules/cjs/loader:1103:14) at Object.Module._extensions..js (node:internal/modules/cjs/loader:1157:10) at Module.load (node:internal/mod...

JS Interview

  https://www.youtube.com/watch?v=_2KyYAzAUOk&t=178s

Find the Highest Altitude

  https://leetcode.com/problems/find-the-highest-altitude/description/

Increment Counter

 Problem Statement=> you have to create two buttons-> Start and Stop when click on the start a counter should start from zero and it should increment every second. when click on stop this counter should pause and on clicking of the start button again resume the counter. If you do like this it will not work and will get stop just after incrementing 1; It's good you are using useRef to persist the value that you are pointing it to. why the state is not updating? SetInterval runs in a different queue and also we are not using useEffect and each and everytime it is getting it value as 0, the loop is running . Please explain why count inside setCount( count + 1 ) is always 0. As set interval runs in different queue, even after state update, it is not able to get the updated count value Why did u use a ref instead of a normal let variable? Variables will be redeclared when components re-render, that is why we either use useState or useRef to store values in React.

Basics

Image
https://leetcode.com/problems/palindrome-linked-list/description/ https://www.w3schools.com/jsref/jsref_slice_array.asp   const arr=[1,2] ; console.log(arr.reverse().join()===arr.join()); ChatGPT The expression arr.reverse() reverses the order of elements in the arr array in place, meaning it modifies the original array. The arr.join() method converts the elements of the array into a string, with each element separated by a comma by default. In the given code, arr.reverse().join() returns a string representation of the reversed array. However, the original arr array has also been reversed because the reverse() method modifies the array directly. Therefore, arr.reverse().join() === arr.join() will evaluate to true because both sides of the comparison refer to the same array that has been reversed and then converted to a string. const newarr =arr; return newarr.reverse().join()===arr.join() ; ChatGPT In the code you provided, newarr is assigned the reference to the arr ...