Number 1 question to solve before FB - Google interview, Array.flat (JS Custom Implementation Ep-3)

preview_player
Показать описание


Custom implementation introduction:

How add custom methods to JavaScript Prototype:

Рекомендации по теме
Комментарии
Автор

to be honest you made this question too simple , thankyou 👍👍

sunnykataria
Автор

Hello friend, you forgot added one case, flat accepts two parameters , array and the second one is depth, so
resolve could be
var flat = function (arr, n = 1 ) {
if(!n) return [...arr]
let arrRes = []
arr.forEach(item => {
if(Array.isArray(item) && n >= 0) {
arrRes.push(...(flat(item, n - 1)))
} else {
arrRes.push(item)
}
})
return arrRes
};

yurypetukhou
Автор

Mphasis interviewer asked me this question (Feb 2022) and at that time I never even knew there is a concept called flattening. I banged my head trying all possible ways to flattening it and failed to do so 😶

saitejagatadi
Автор

Well I need some tips well should I complete the whole front-end first then move to backend or should I do them side by side, I am a second year student

Mdenterprises