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

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

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)
@saumyaranjannayak2101
@saumyaranjannayak2101 Ай бұрын
If someone finds reduce complex, try the below simple for loop soln: function flatten(arr, anss){ let ans = [] for(let i = 0; i
@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 Жыл бұрын
@@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 Жыл бұрын
@@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 Жыл бұрын
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 Жыл бұрын
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 Жыл бұрын
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 ....
@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 Жыл бұрын
const res=[] function flat(arr){ for(let i=0;i
@PIYUSH-lz1zq
@PIYUSH-lz1zq Жыл бұрын
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 Жыл бұрын
@@PIYUSH-lz1zq else part will act as base cond, keeps on pushing and moving forward to next element
@PIYUSH-lz1zq
@PIYUSH-lz1zq Жыл бұрын
@@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 26 күн бұрын
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 Жыл бұрын
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 ....
Who's spending her birthday with Harley Quinn on halloween?#Harley Quinn #joker
01:00
Harley Quinn with the Joker
Рет қаралды 22 МЛН
Amazing remote control#devil  #lilith #funny #shorts
00:30
Devil Lilith
Рет қаралды 14 МЛН
Каха и лужа  #непосредственнокаха
00:15
Flatten an array - frontend/Javascript Interview Question
7:43
Interview.Js
Рет қаралды 13 М.
Flatten Array & Object in JavaScript || Interview Question
21:31
Who's spending her birthday with Harley Quinn on halloween?#Harley Quinn #joker
01:00
Harley Quinn with the Joker
Рет қаралды 22 МЛН