Q3 Flatten an Array | Flipkart Frontend Interview Questions | Javascript Questions for Interviews

  Рет қаралды 4,148

JsCafe

JsCafe

Күн бұрын

Пікірлер: 19
@ayushkushwaha171
@ayushkushwaha171 Жыл бұрын
awesome, one more variation could be where you can give level of depth to which flat occurs to your function like flat(arr,2)
@nityamgosain
@nityamgosain 2 жыл бұрын
const flat =(arr)=> arr.toString().split(',').map((el)=>Number(el))
@akshaykumar5012
@akshaykumar5012 2 жыл бұрын
This will not work for all cases
@yzo954
@yzo954 2 жыл бұрын
this will not work if you run this function for [ 'hello, world',['something,test'] ] => since you are splitting the converted arr string using comma (,) resulted output will be ['hello', 'world','something','test']
@PIYUSH-lz1zq
@PIYUSH-lz1zq 2 жыл бұрын
@@akshaykumar5012 while pushing the actual output to res after that we should empty the curr array so taht fresh val dont append with previous values ...... what we used to do in c++/java !!! please tell ....
@PIYUSH-lz1zq
@PIYUSH-lz1zq 2 жыл бұрын
@@yzo954 while pushing the actual output to res after that we should empty the curr array so taht fresh val dont append with previous values ...... what we used to do in c++/java !!! please tell ....
@sachinmalik5837
@sachinmalik5837 2 жыл бұрын
Hi, Very nice explanation for the flow. I am commenting a solution without Reduce so if someone doesn't fully understand Reduce can still do this one of the most famous frontend question const flattenArray = (intialArray) => { var result = []; for(let i=0;i
@PIYUSH-lz1zq
@PIYUSH-lz1zq 2 жыл бұрын
Bro , when making recursive call to nth level ... why you didn't write base conditions to come back from last level of nesting ?
@PIYUSH-lz1zq
@PIYUSH-lz1zq 2 жыл бұрын
while pushing the actual output to res after that we should empty the curr array so taht fresh val dont append with previous values ...... what we used to do in c++/java !!! please tell ....
@saumyaranjannayak2101
@saumyaranjannayak2101 2 ай бұрын
If someone finds reduce complex, try the below simple for loop soln: function flatten(arr, anss){ let ans = [] for(let i = 0; i
@NikhilSharma-r1v
@NikhilSharma-r1v Жыл бұрын
function flattenArr(arr) { const result = []; arr.forEach((element) => { if (Array.isArray(element)) { const nestedFlatArr = flattenArr(element); result.push(...nestedFlatArr); } else { result.push(element); } }); return result; }
@amansaxena4446
@amansaxena4446 2 жыл бұрын
const res=[] function flat(arr){ for(let i=0;i
@PIYUSH-lz1zq
@PIYUSH-lz1zq 2 жыл бұрын
Bro , when making recursive call to nth level ... why you didn't write base conditions to come back from last level of nesting ?
@amansaxena4446
@amansaxena4446 2 жыл бұрын
@@PIYUSH-lz1zq else part will act as base cond, keeps on pushing and moving forward to next element
@PIYUSH-lz1zq
@PIYUSH-lz1zq 2 жыл бұрын
@@amansaxena4446 while pushing the actual output to res after that we should empty the curr array so taht fresh val dont append with previous values ...... what we used to do in c++/java !!! please tell ....
@pranavJha93
@pranavJha93 Ай бұрын
let myArr = []; function flat(element){ for(const el of element){ if(typeof el ==='object'){ flat(el); } else myArr.push(el); } } flat(arr)
@venkateshpachigulla
@venkateshpachigulla 2 жыл бұрын
We can achieve this using Array flat method like console.log(Array.flat(arr, Infinity)). We can consider your code as a polyfill for Array flat method.
@js_cafe
@js_cafe 2 жыл бұрын
Yes, but refrain from using flat method in interview, as the interviewer will not be seeking you to use in built helper functions.
@PIYUSH-lz1zq
@PIYUSH-lz1zq 2 жыл бұрын
while pushing the actual output to res after that we should empty the curr array so taht fresh val dont append with previous values ...... what we used to do in c++/java !!! please tell ....
Accompanying my daughter to practice dance is so annoying #funny #cute#comedy
00:17
Funny daughter's daily life
Рет қаралды 25 МЛН
Core Java Coding Interview Questions and Answers
13:50
Coding Shinobi
Рет қаралды 206
Making an Algorithm Faster
30:08
NeetCodeIO
Рет қаралды 160 М.
Accompanying my daughter to practice dance is so annoying #funny #cute#comedy
00:17
Funny daughter's daily life
Рет қаралды 25 МЛН