Posts

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...